Marin Drago
Marin Drago

Reputation: 11

Word VBA: subaddress:= to the first bookmark

i am currently "writing" something that creates two bookmarks in my word document. my intention is to create one bookmark in my text and a second bookmark at the end of the document. there i will insert a picture as kind of a proof to my argument in the text. so far it worked out. i need a solution for an automation of the hyperlink. i want to create a hyperlink from the second bookmark back to the first bookmark and back again. this will allow the reader to jump from the text and to the proof and back again. i want to create several of theese bookmarks. the bookmarks have changing names that are equivalent to the selected words in the text. so i need a variable, i think.

This is my code so far, pleas be kind:

sub bkmrk

Dim s As String

Selection.Words(1).Select
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy

s = Selection
    With ActiveDocument.Bookmarks
    .Add Name:=s + "zurückneu", Range:=Selection.Range
       .DefaultSorting = wdSortByName
       .ShowHidden = False
    End With

Selection.EndKey Unit:=wdStory
Selection.InsertBreak Type:=wdPageBreak
Selection.Paste


Dim b As String





Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Words(1).Select
Selection.Copy

b = Selection
    With ActiveDocument.Bookmarks
    .Add Name:=b + "hinneu", Range:=Selection.Range
       .DefaultSorting = wdSortByName
       .ShowHidden = False
    
End With

end sub``` 

Upvotes: 0

Views: 323

Answers (2)

Marin Drago
Marin Drago

Reputation: 11

i got it. Thanks. Here is my result.

Option Explicit

Sub bkmrk()
Dim s As String

Selection.Words(1).Select
Selection.MoveLeft unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Copy

s = Selection
With ActiveDocument.Bookmarks
.Add Name:=s + "zurückneu", Range:=Selection.Range
   .DefaultSorting = wdSortByName
   .ShowHidden = False
End With

Selection.EndKey unit:=wdStory
Selection.InsertBreak Type:=wdPageBreak

ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
  SubAddress:=s + "zurückneu", ScreenTip:="", TextToDisplay:=s 'Selection.Range

Dim b As String

Selection.MoveLeft unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Words(1).Select
Selection.Copy

b = Selection
With ActiveDocument.Bookmarks
.Add Name:=b + "hinneu", Range:=Selection.Range
   .DefaultSorting = wdSortByName
   .ShowHidden = False

End With

Selection.GoTo What:=wdGoToBookmark, Name:=s + "zurückneu"
    With ActiveDocument.Bookmarks
        .DefaultSorting = wdSortByName
        .ShowHidden = False
    End With

Selection.MoveRight unit:=wdCharacter, Count:=1
Selection.Words(1).Select

ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
  SubAddress:=b + "hinneu", ScreenTip:="", TextToDisplay:=b 'Selection.Range

End Sub

Upvotes: 1

macropod
macropod

Reputation: 13490

The simplest solution remains adding the hyperlink forward/backward buttons to the QAT. This can be done by modifying each user's Word.officeUI file, which is typically located in:

C:\Users\%UserName%\AppData\Local\Microsoft\Office

Word.officeUI is a plain text file, which you can edit with VBA. There are plenty of solutions available for modifying text files. What you need to look for are the strings:

  • <mso:control idQ="mso:WebGoBack"; and
  • <mso:control idQ="mso:WebGoForward"

If missing, add the entries. Typical entries look like:

<mso:control idQ="mso:WebGoBack" visible="true" insertBeforeQ="mso:PrintPreviewAndPrint"/>
<mso:control idQ="mso:WebGoForward" visible="true" insertBeforeQ="mso:PrintPreviewAndPrint"/>

where

  • insertBeforeQ="mso:PrintPreviewAndPrint"

tells Word where to position the icons on the QAT (in this case, before the Print Preview icon).

This is a once-off exercise for each user and will be available to all documents without the need to modify a single document.

Upvotes: 0

Related Questions