vba - Hyperlinked text in Word footers to a selected bookmark -
i wanted custom footer in documents hyperlinked text bookmark in same document. i.e. 'top of document' kind of link in footers. had collect information on places achieve much. , wanted share here others not have fight thing @ once.
so far question & suggestions stackoverflow , other sites, have achieved much-
- created macro create bookmark automatically, of selected text in document.
- bookmark re-created (delete , create) if present
- macro add new footer page number , text delimiter (i.e. / hit overview).
now want create text in footer hyperlink bookmark. code simple. guess doing wrong, tried creating hyperlink object. not working. please suggest something.
here macro function-
sub insertfootnote() const wdalignpagenumbercenter = 1 dim varnumberpages variant varnumberpages = activedocument.content.information(wdactiveendadjustedpagenumber) ' delete bookmark if name if activedocument.bookmarks.exists("hitoverviewmac") = true activedocument.bookmarks("hitoverviewmac").delete end if ' create bookmark selected text activedocument.bookmarks .add range:=selection.range, name:="hitoverviewmac" .defaultsorting = wdsortbyname .showhidden = false end dim mhlink hyperlink dim long = 1 activedocument.sections.count activedocument.sections(i) ' remove footer '.footers(wdheaderfooterprimary).range.text = "" '.footers(wdheaderfooterprimary).pagenumbers.add (wdalignpagenumbercenter) '.footers(wdheaderfooterprimary).range.insertbefore "hit overview / page " .footers(wdheaderfooterprimary).range.select selection if activedocument.bookmarks.exists("hitoverviewmac") = true .paragraphs(1).alignment = wdalignparagraphcenter .typetext text:="page " .fields.add range:=selection.range, type:=wdfieldempty, text:= _ "page ", preserveformatting:=true .typetext text:=" of " .fields.add range:=selection.range, type:=wdfieldempty, text:= _ "numpages ", preserveformatting:=true .endkey unit:=wdline .typetext text:=" ~ " activedocument.hyperlinks.add anchor:=.range, address:="", _ subaddress:="hitoverview", screentip:="", texttodisplay:="hit overview" else msgbox "bookmark not exists" end if end end next end sub
ok, wasn't problem macro(except below), problem couple of documents testing with. few mistakes missed - subaddress:="bookmark_name" , anchor:=selection.range.
so problem occurs if doc has text in footers. , removing footer first.
here code everyone's reference-
sub insertfootnote() const wdalignpagenumbercenter = 1 dim varnumberpages variant varnumberpages = activedocument.content.information(wdactiveendadjustedpagenumber) if activedocument.bookmarks.exists("hitoverviewmac") = true activedocument.bookmarks("hitoverviewmac").delete end if activedocument.bookmarks .add range:=selection.range, name:="hitoverviewmac" .defaultsorting = wdsortbyname .showhidden = false end dim mhlink hyperlink dim long = 1 activedocument.sections.count activedocument.sections(i) .footers(wdheaderfooterprimary).range.text = "" .footers(wdheaderfooterprimary).pagenumbers.add (wdalignpagenumbercenter) .footers(wdheaderfooterprimary).range.select selection if activedocument.bookmarks.exists("hitoverviewmac") = true .paragraphs.alignment = wdalignparagraphcenter .typetext text:="page " .fields.add range:=selection.range, type:=wdfieldempty, text:= _ "page ", preserveformatting:=true .typetext text:=" of " .fields.add range:=selection.range, type:=wdfieldempty, text:= _ "numpages ", preserveformatting:=true .endkey unit:=wdline .typetext text:=" / " activedocument.hyperlinks.add anchor:=selection.range, address:="", _ subaddress:="hitoverviewmac", screentip:="", texttodisplay:="hit overview" else msgbox "bookmark not exists" end if end end next end sub
Comments
Post a Comment