Bookmarks with the wrong name

O

Oz

Hi,

I've written a macro that will edit my bookmark addresses to a link in
the same document using a simple search then replace loop. The problem
is, the new addresses point to a place that doesn't exist - the place
in the file is right, but the file name itself isn't there. For
example, the link might turn out to be:

C:\Desktop\Bookmark_name

So the file isn't actually in there!

My code is:

For Each oBookmark In ActiveDocument.Bookmarks
oBookmarkName = oBookmark.Name

If InStr(1, oBookmarkName,HLinkName, vbTextCompare) = 1
Then
MsgBox oBookmarkName
ActiveDocument.Hyperlinks(i).Address =
oBookmarkName

End If
Next oBookmark

Thanks!
 
O

Oz

Ok, thanks. But I now get an error whenever I try to run the macro. My
full code is:

Sub ConvertHLinks()

Dim i As Integer
Dim HLinkName As String
Dim oBookmark As Bookmark
Dim oBookmarkName As String

For i = 1 To ActiveDocument.Hyperlinks.Count
HLinkName = ActiveDocument.Hyperlinks(i).TextToDisplay
HLinkName = Replace(HLinkName, " ", "_")
numWords = UBound(Split(HLinkName, " ")) + 1
HLinkName = Left(HLinkName, InStr(HLinkName, "(") - 2)

For Each oBookmark In ActiveDocument.Bookmarks
oBookmarkName = oBookmark.Name

If InStr(1, oBookmarkName,HLinkName, vbTextCompare) = 1
Then
MsgBox oBookmarkName
ActiveDocument.Hyperlinks(i).A­ddress =
oBookmarkName


End If
Next oBookmark
Next
End Sub

I just don't understand what is wrong! The correct bookmarks are there,
I just can't get all they hyperlinks to match up with them.
 
D

Dave Lett

Hi Oz,

It doesn't look like you change Address to SubAddress.

HTH,
Dave

Ok, thanks. But I now get an error whenever I try to run the macro. My
full code is:

Sub ConvertHLinks()

Dim i As Integer
Dim HLinkName As String
Dim oBookmark As Bookmark
Dim oBookmarkName As String

For i = 1 To ActiveDocument.Hyperlinks.Count
HLinkName = ActiveDocument.Hyperlinks(i).TextToDisplay
HLinkName = Replace(HLinkName, " ", "_")
numWords = UBound(Split(HLinkName, " ")) + 1
HLinkName = Left(HLinkName, InStr(HLinkName, "(") - 2)

For Each oBookmark In ActiveDocument.Bookmarks
oBookmarkName = oBookmark.Name

If InStr(1, oBookmarkName,HLinkName, vbTextCompare) = 1
Then
MsgBox oBookmarkName
ActiveDocument.Hyperlinks(i).A­ddress =
oBookmarkName


End If
Next oBookmark
Next
End Sub

I just don't understand what is wrong! The correct bookmarks are there,
I just can't get all they hyperlinks to match up with them.
 
J

Jonathan West

Like I said, you need to set the *SubAddress* porperty of the Hyperlink
object, not the Address!

Change this line

ActiveDocument.Hyperlinks(i).A­ddress = oBookmarkName


to this

ActiveDocument.Hyperlinks(i).SubA­ddress = oBookmarkName

--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Keep your VBA code safe, sign the ClassicVB petition www.classicvb.org

Ok, thanks. But I now get an error whenever I try to run the macro. My
full code is:

Sub ConvertHLinks()

Dim i As Integer
Dim HLinkName As String
Dim oBookmark As Bookmark
Dim oBookmarkName As String

For i = 1 To ActiveDocument.Hyperlinks.Count
HLinkName = ActiveDocument.Hyperlinks(i).TextToDisplay
HLinkName = Replace(HLinkName, " ", "_")
numWords = UBound(Split(HLinkName, " ")) + 1
HLinkName = Left(HLinkName, InStr(HLinkName, "(") - 2)

For Each oBookmark In ActiveDocument.Bookmarks
oBookmarkName = oBookmark.Name

If InStr(1, oBookmarkName,HLinkName, vbTextCompare) = 1
Then
MsgBox oBookmarkName
ActiveDocument.Hyperlinks(i).A­ddress =
oBookmarkName


End If
Next oBookmark
Next
End Sub

I just don't understand what is wrong! The correct bookmarks are there,
I just can't get all they hyperlinks to match up with them.
 

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