Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    New Lounger
    Join Date
    Jul 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Converting all my hyperlinks (Word 2003)

    Hi, I'm new here, so I hope I've posted this in the right place!

    I've converted a PFD file to Word. The file contains hundreds of hyperlinks that point to places in the same file. My problem is that these hyperlinks all point to the header of the target page, rather than the target itself - a result of the conversion.

    So I firstly wrote a macro that finds all the headings and subheadings and converts them into bookmarks. This seems to work quite well, but not all the headings are picked up, even though they all have the same font. I then wrote a macro that took all the hyperlinks, compared them to the bookmarks, and if there was a match, stored the bookmark as the new subaddress of the hyperlink.

    However, I don't seem to be able to get this second part working. I've never studied VBA before, so its all getting rather confusing for me! I was wondering if anyone could look at my code and tell me where I'm going wrong?

    Sub GetBookmarks()

    Dim SenRange As Range
    Dim j As Integer
    Dim Addy As String

    On Error Resume Next
    For Each SenRange In ActiveDocument.Sentences
    If SenRange.Font.Name = "Helvetica" Then
    SenRange.HighlightColorIndex = wdYellow
    Addy = SenRange.text
    Addy = Replace(Addy, " ", "_")
    'MsgBox Addy
    ActiveDocument.Bookmarks.Add Name:=Addy, Range:=SenRange

    End If
    Next

    End Sub

    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).SubAddress = oBookmarkName

    End If
    Next oBookmark
    Next

    End Sub

    Many thanks, Oliver

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Converting all my hyperlinks (Word 2003)

    This document sounds like quite a mess. <img src=/S/grin.gif border=0 alt=grin width=15 height=15> What converter did you use? Maybe there's a better one available?

    Anyway, regarding your code, I'm not sure where the breakdown is. If you insert a Stop command just before the "For Each oBookmark In ActiveDocument.Bookmarks" loop, and check the Locals window, does HLinkName match any existing bookmark that you know of?

    (I just noticed that your HLinkName code gets rid of all the spaces, replacing them with _ characters, but then tries to extract out part of the name by looking at the spaces. This could be a problem.)

  3. #3
    New Lounger
    Join Date
    Jul 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Converting all my hyperlinks (Word 2003)

    Well my aim was to find a decent PDF to doc converter, and this was the best one there was! All of the others totally ignored the hyperlinks.

    That last point you mentioned was just me forgetting to remove the nunmWords line. That shouldn't be there.

    That stop thing was incredibly helpful though! My two main problems seem to be:

    1) Replacing the spaces with underscores to match a correct bookmark sometimes do this: _This_is_a_bookmark, whereas the stored bookmark looks like this: This_is_a_bookmark. So obviously they don't match. How can I get ride of the first _?

    2) Not all the headings are going in as bookmarks. Are there a limit on the number of bookmarks allowed?

    Thanks again for your help, I appreciate it. I've been working on this for the best part of a week now!

  4. #4
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Salt Lake City, Utah, USA
    Posts
    9,508
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Re: Converting all my hyperlinks (Word 2003)

    1. HLinkName = Replace(Trim(HLinkName), " ", "_")

    2. No idea.
    -John ... I float in liquid gardens
    UTC -7ąDS

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Converting all my hyperlinks (Word 2003)

    Regarding #2, Microsoft publishes a list of various maximum numbers and sizes of things for each version of Word. You might find a link searching on the Word board.

  6. #6
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Converting all my hyperlinks (Word 2003)

    Try <!mskb=211489>Microsoft Knowledge Base Article 211489<!/mskb>
    <pre>Number of bookmarks
    per document 16,379
    </pre>

    StuartR

  7. #7
    New Lounger
    Join Date
    Jul 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Converting all my hyperlinks (Word 2003)

    Well I definately haven't got that many! Thanks very much for all the help. If I have any more problems I won't hesitate to ask [img]/forums/images/smilies/smile.gif[/img]

  8. #8
    New Lounger
    Join Date
    Jul 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Converting all my hyperlinks (Word 2003)

    Hmm, another problem with converting my headings into bookmarks. None of the headings are accepted as bookmarks, even when I use rTrim, and Trim, and lots of other things! When stepping through the program I can see a little box at the end of each of my headings. I guess this is a carriage return. What code will get rid of these?

    Many thanks!

  9. #9
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Converting all my hyperlinks (Word 2003)

    <P ID="edit" class=small>(Edited by jscher2000 on 20-Jul-05 16:51. )</P>
    In this line --

    Addy = SenRange.text

    -- assuming your code hasn't changed a whole lot, you probably are picking up the "end of range" marker, in this case a CRLF pair. Just before that try this line:

    SenRange.MoveEnd wdCharacter, -1

    If -1 doesn't do it, try -2. <img src=/S/grin.gif border=0 alt=grin width=15 height=15> Obviously there are other techniques, but this one is reasonably fast to write and conceptually simple to debug.

    Added: Upon further review of your code, I see that SenRange is part of your loop logic. Please don't accidentally create an infinite loop without first saving your work!

  10. #10
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Converting all my hyperlinks (Word 2003)

    Here is a function that I use to convert a string to a valid name for a bookmark, you could add this to your module and then replace
    <code>Addy = SenRange.text</code>

    with
    <code>Addy = StrToBookmark(SenRange.text)</code>


    StuartR

    <code>
    Private Function StrToBookmark(strInput As String) As String

    Dim strTemp As String
    Dim strChar As String
    Dim i As Integer
    Dim blStarted As Boolean

    For i = 1 To Len(strInput)
    strChar = Mid(strInput, i, 1)
    Select Case strChar
    Case "a" To "z", "A" To "Z"
    strTemp = strTemp & strChar
    blStarted = True
    Case "0" To "9", "_"
    If blStarted Then strTemp = strTemp & strChar
    Case Else
    End Select
    Next i
    StrToBookmark = Left(strTemp, 31)
    End Function
    </code>

  11. #11
    New Lounger
    Join Date
    Jul 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Converting all my hyperlinks (Word 2003)

    Thanks Stuart! Before I had a load of replace statements but that works perfectly.

    jscher, I used Addy = Replace(Addy, Chr(13), "") in the end, which worked well. That was obviously made redundant when Stuart posted [img]/forums/images/smilies/wink.gif[/img]

    So I now have a program that converts all headings into bookmarks. It then finds all the hyperlinks, converts them into the same format as the bookmarks, and then compares itself to the bookmarks until it finds the right one. Then its supposed to use the bookmark name as the subaddress of hyperlink. But this never works! I always get an Error -2147467259, which I assume means they don't have a clue what the problem is!

    My last bit of code is:

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


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

    End If
    Next oBookmark

    Can anyone see where I might be going wrong? I just don't understand why it doesn't work!

    (oh, and a sample bookmark would be: thisisabookmark)

  12. #12
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Converting all my hyperlinks (Word 2003)

    Just for laughs, maybe pump out a list of the things you're comparing into a blank document:

    dim docTarget as word.document
    Set docTarget = documents.add

    docTarget.content.insertafter stringvar & vbCrLf

    Sort both lists, then paste into a two-column table side-by-side (or use Excel). Do they match?

  13. #13
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Converting all my hyperlinks (Word 2003)

    I don't think I've ever tried to modify hyperlink.SubAddress and when I tried just now from the Immediate window I couldn't get it to work either.

    StuartR

  14. #14
    New Lounger
    Join Date
    Jul 2005
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Converting all my hyperlinks (Word 2003)

    jscher, they match. I use the same function to get the string, so they are the same. I have it partially working now. I used the stop command, and I put it just before the subaddress line. Most of the bookmarks do actually work now, its just sometimes I get an error 5, whatever that is. Only every 20 hyperlinks or so though.

    The code I use, and which works, is:

    ActiveDocument.Hyperlinks(i).Address = "c:edit.doc"
    ActiveDocument.Hyperlinks(i).SubAddress = oBookmarkName

    however this obviously isn't going to work when I change the directory. Guess I'll just have to change it manually!

    Thanks for the help guys. This is the first project I was set since I started my internship 2 weeks ago, and I'm still working on it!

  15. #15
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Converting all my hyperlinks (Word 2003)

    The other day I wanted to quickly fix some hyperlinks to bookmarks in the same document, so I used Alt+F9 to toggle and edited the fields directly. After toggling back, I discovered: It had no effect! Clearly, Hyperlinks are a strange beast.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •