Search code examples
vbams-wordhyperlinkbookmarks

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``` 

Solution

  • 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