Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2005

Tip: Looking for answers? Try searching our database.

Updating hyperlink pointers

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Oz - 14 Jul 2005 21:29 GMT
Hi,

I've just converted a large manual from PDF into Word. This manual contains
hundreds of hyperlinks to other pages in the same manual.  My problem is that
the PDF converted converted each of these hyperlinks to point to the top of
the target page, rather than the target itself.

Obviously to go through and edit every single hyperlink would take forever,
so I put all the headings and subheadings into a table of contents, so they
can be directly linked to.

My problem is that I somehow need to, for each link, look up the bookmark
with the same name, and replace the old hyperlink target with the bookmark.
Is this at all possible? I haven't looked at any VB for a couple of years,
but I'd write a script if someone gave me some pointers.

Many thanks.
Doug Robbins - 14 Jul 2005 23:21 GMT
This is not exactly what you want, but may give you a few ideas:

' Macro created 26/10/01 by Doug Robbins to update links in a document
'
Dim alink As Field, linktype As Range, linkfile As Range
Dim linklocation As Range, i As Integer, j As Integer, linkcode As Range
Dim Message, Title, Default, Newfile
Dim counter As Integer

counter = 0
For Each alink In ActiveDocument.Fields
   If alink.Type = wdFieldLink Then

       Set linkcode = alink.Code
       i = InStr(linkcode, Chr(34))
       Set linktype = alink.Code
       linktype.End = linktype.Start + i
       j = InStr(Mid(linkcode, i + 1), Chr(34))
       Set linklocation = alink.Code
       linklocation.Start = linklocation.Start + i + j - 1
       If counter = 0 Then
           Set linkfile = alink.Code
           linkfile.End = linkfile.Start + i + j - 1
           linkfile.Start = linkfile.Start + i
           Message = "Enter the modified path and filename following this
Format " & linkfile
           Title = "Update Link"
           Default = linkfile
           Newfile = InputBox(Message, Title, Default)
       End If
       linkcode.Text = linktype & Newfile & linklocation
       counter = counter + 1
   End If
Next alink

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Hi,
>
[quoted text clipped - 19 lines]
>
> Many thanks.
Oz - 14 Jul 2005 23:46 GMT
Ok. Looks complicated, but I'll give it a try! I had a go at writing a
method myself. Would this work at all?

Sub ConvertHLinks()

Dim i As Integer
Dim HLinkName As String

If ActiveDocument.Hyperlinks.Count > 0 Then
       For i = 1 To ActiveDocument.Hyperlinks.Count
           HLinkName = ActiveDocument.Hyperlinks(i).TextToDisplay

           ' Crop text???'

           ActiveDocument.Hyperlinks(i).Address =
BookmarkSearch(HLinkName)
Next
       MsgBox "All Done"

   End If

End Sub

Function BookmarkSearch(HLinkName As String)

Dim oBookmark As Bookmark
Dim oBookmarkName As String

For Each oBookmark In ActiveDocument.Bookmarks
       oBookmarkName = oBookmark.Name
       If InStr(1, oBookmarkName, HLinkName, vbTextCompare) = 1 Then
           Return oBookmarkName
       End If
   Next oBookmark

End Function

--------

The only bit I'm missing out is 'Crop text?'. If I have a phrase, eg.
add hyperlinks to thread (page 2-3), and I know that the bookmark is
stored under "add hyperlinks to thread", what method can I use on the
phrase to get rid of the (page 2-3) bit?

thanks again!
Doug Robbins - 15 Jul 2005 06:30 GMT
Use

Dim HlinkName As String
HlinkName = "add hyperlinks to thread (page 2-3)"
HlinkName = Left(HlinkName, InStr(HlinkName, "(") - 2)
MsgBox HlinkName

The last line demonstrates the effect

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Ok. Looks complicated, but I'll give it a try! I had a go at writing a
> method myself. Would this work at all?
[quoted text clipped - 41 lines]
>
> thanks again!
Oz - 15 Jul 2005 18:28 GMT
Thanks for your help, Doug, I really appreciate it.

I'm a bit confused how I'm supposed to run this script though. I put it
into a module window in VBA for the document I want to use. But when I
click play it says The Macros in this project are disabled. It says
this regardless of the security settings I use.
Oz - 15 Jul 2005 18:33 GMT
Sorry, my bad. I got past that. But when I run the macro I get an error
saying Run-time error '5'. Invalid procedure call or argument, on the
line:

ActiveDocument.Hyperlinks(i).Address = BookmarkSearch(HLinkName)

where BookmarkSearch is a function that takes the string HLinkName and
returns a string.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.