MS Word Hyperlink thru code

D

Derek Howard

I have a hyperlink on page one that jump to text formated as a header on page
two. After a merge the link is broken and the hyperlink goes nowhere. Below
are the manual instructions that I have to do to restore the link.

1 - Scroll down to hyperlink labeled - 'Important Safety Information'
2 - Right click on the hyperlink and select 'Edit Hyperlink'
3 - Select 'Place in This Document'
4 - Select 'Important Safety Information' under Headings
5 - Click the 'Ok' button

I need to automate this thru code to restore what the merge deleted. It is a
merge of only one record.
 
M

macropod

Hi Derek,

For a single merged record, you could use a macro like:
Sub RestoreHyperlink()
Dim StrTxt, HBkMrk As String
StrTxt = "Important Safety Information"
HBkMrk = "_" & Replace(StrTxt, " ", "_")
With Selection.Find
.ClearFormatting
.Style = ActiveDocument.Styles("Heading 1")
.Text = StrTxt
.Replacement.Text = ""
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
If .Found Then
ActiveDocument.Bookmarks.Add Name:=HBkMrk, Range:=.Parent.Range
Selection.HomeKey
.Style = ActiveDocument.Styles("Normal")
.Execute
If .Found Then _
.Parent.Hyperlinks.Add Anchor:=.Parent.Range, Address:="", _
SubAddress:=ActiveDocument.Bookmarks(HBkMrk), ScreenTip:="", TextToDisplay:=StrTxt
End If
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top