Search for a certain style in word 2010 and make it into a bookmark using vba -
how make style bookmark in word 2010?

you won't able use of text in document bookmark name. illegal use characters in bookmark name in word/vba. may possible add such characters in bookmark names in xml format of document, if required, can ask separate question.
this feels way code post on so. need explain framework have in place , tell hurdles are. can't again. "works me". if have questions though don't hesitate ask.
run "runme" macro @ bottom.
private function isparagraphstyledwithheading(para paragraph) boolean dim flag boolean: flag = false if instr(1, para.style, "heading", vbtextcompare) > 0 flag = true end if isparagraphstyledwithheading = flag end function private function gettextrangeofstyledparagraph(para paragraph) string dim textofrange string: textofrange = para.range.text gettextrangeofstyledparagraph = textofrange end function private function bookmarknamealreadyexist(bookmarkname string) boolean dim bookmark bookmark dim flag boolean: flag = false each bookmark in activedocument.bookmarks if bookmarkname = bookmark.name flag = true end if next bookmarknamealreadyexist = flag end function private function createuniquebookmarkname(bookmarkname string) dim uniquebookmarkname string dim guid string: guid = mid$(createobject("scriptlet.typelib").guid, 2, 36) guid = replace(guid, "-", "", , , vbtextcompare) uniquebookmarkname = bookmarkname & guid createuniquebookmarkname = uniquebookmarkname end function private function bookmarkit(rng range, bookmarkname string) dim cleanname string: cleanname = makevalidbmname(bookmarkname) if bookmarknamealreadyexist(cleanname) cleanname = createuniquebookmarkname(cleanname) end if activedocument.bookmarks.add name:=cleanname, range:=rng end function ''shamelessly stolen gmaxey @ http://www.vbaexpress.com/forum/showthread.php?t=37674 private function makevalidbmname(strin string) dim pfirstchr string dim long dim tempstr string strin = trim(strin) pfirstchr = left(strin, 1) if not pfirstchr "[a-za-z]" strin = "a_" & strin end if = 1 len(strin) select case asc(mid$(strin, i, 1)) case 49 58, 65 90, 97 122 tempstr = tempstr & mid$(strin, i, 1) case else tempstr = tempstr & "_" end select next tempstr = replace(tempstr, " ", " ") makevalidbmname = tempstr end function sub runme() dim para paragraph dim textofpara string each para in activedocument.paragraphs if isparagraphstyledwithheading(para) textofpara = gettextrangeofstyledparagraph(para) if para.range.bookmarks.count < 1 bookmarkit para.range, textofpara end if end if next end sub
Comments
Post a Comment