Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi all,

    Word 2003 VBA

    I have a reference which includes a hyperlink

    (which was inserted using Insert > Reference > Cross-reference)

    and I would like to use VBA to "follow it" in a similar way you can follow a hyperlink,

    e.g., like this: Selection.Hyperlinks(1).Follow.

    Note: this is not a reference to a bookmark, i.e., it is of the form:

    { REF _Ref240109123 \h }

    Is there a way to do this?

    TIA

    - avi

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts
    Avi,

    Actually the reference is to a bookmark, it's just a hidden one - so if you go to Insert > Bookmarks, and put a checkmark next to 'Hidden Bookmarks', you'll see the names that Word has automatically assigned to each cross-ref that you've entered. (You'll see bookmark names like "_Ref240178640").

    So what the VBA code needs to do is extract that bookmark name from the cross-ref field code, and then select that bookmark in the document. Based on a quick test, code like the following should work (again, not tested extensively):
    Code:
    Sub FollowXRefLink()
    
       Dim strRefBmk As String
       strRefBmk = Selection.Sentences(1).Fields(1).Code
    
       'Get rid of characters before and after the ref number:
       strRefBmk = Right$(strRefBmk, Len(strRefBmk) - 5)
       strRefBmk = Left$(strRefBmk, Len(strRefBmk) - 4)
    
       ActiveDocument.Bookmarks(strRefBmk).Select
    
    End Sub
    One note here: This code will work if the user is clicked in or has selected any part of the sentence that contains the cross-reference (the code assumes that there is only one cross-reference in a given sentence). If you anticipate there being more than one cross-reference in a sentence, then the code that selects the cross-reference would need to be refined further.

    Gary

  3. #3
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Sorry all about the cross-post.

    I read the instructions only afterwards and then decided i should xfer this post to the Word forum, but then forgot to delete it from this forum.




    [quote name='amakeler' post='792535' date='08-Sep-2009 15:15']Hi all,

    Word 2003 VBA

    I have a reference which includes a hyperlink

    (which was inserted using Insert > Reference > Cross-reference)

    and I would like to use VBA to "follow it" in a similar way you can follow a hyperlink,

    e.g., like this: Selection.Hyperlinks(1).Follow.

    Note: this is not a reference to a bookmark, i.e., it is of the form:

    { REF _Ref240109123 \h }

    Is there a way to do this?

    TIA

    - avi[/quote]

  4. #4
    2 Star Lounger
    Join Date
    Dec 2007
    Posts
    179
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Looks great, Gary. Thanks!

    So I have that "hidden bookmarks" checkbox just 12 inches from my nose for years and forgot it was there...

    Oh my...

    And I am supposed to be a Word "expert".

    Great thanks!

    - avi

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts
    [quote name='amakeler' post='792564' date='08-Sep-2009 19:25']Sorry all about the cross-post.

    I read the instructions only afterwards and then decided i should xfer this post to the Word forum, but then forgot to delete it from this forum.[/quote]
    No problem, thanks for the explanation.

Posting Permissions

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