Consulting

Results 1 to 9 of 9

Thread: Code to put hyperlinks in automatically

  1. #1
    VBAX Contributor
    Joined
    Oct 2004
    Location
    Cardiff
    Posts
    120
    Location

    Code to put hyperlinks in automatically

    Hi,

    Assuming it possible to divide a document into two sections manually, is it possible to get a macro to search through the first part of the document and, if it finds words included in parentheses (like these are), then look for an identical set of words, also in parentheses, in the second part of the document, and turn them into hyperlinks that take you from the one to the other and back when clicked?

    Here's hoping for a miracle.

    Damo
    You cannot solve all the world's major problems using only potatoes. :- Adams

    http://www.vbaexpress.com

  2. #2
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Yes, it is possible. It would not be a trivial thing, but absolutely, yes it is possible.

    There are some design needs that you must determine.

    Say you have 14 occurences of your text in Section 1- whatever it is - and for the sake of argument, 14 in Section 2. What do you want to happen? Occurence 1 Section 1 links with occurence 1 Section 2, occurence 2 Section 1 links with occurence 2 Section 2?

    You MUST determine the logic behind this.

    But can you do a search for text, and turn the found text into a hyperlink? Yes. Can you dod a search for text in one section and see if it has a matchig text in a different section? Yes.

    You need to spec out EXACTLY, with zero fuzzyness PRECISELY what you want to happen. You question is too imprecise, and so it gets the answer:

    Yes, you can. You can start with playing with:
    [vba]Sub SearchMakeHyper()
    ' make range object section 1
    ' select it and search
    Dim r As Range
    Set r = ActiveDocument.Sections(1).Range
    r.Select
    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "(in parenthesis)"
    .Execute
    If .Found = True Then
    With ActiveDocument.Bookmarks
    .Add Range:=Selection.Range, Name:="myHyper1"
    End With
    End If
    End With
    Selection.Collapse direction:=wdCollapseStart
    Set r = Nothing
    Set r = ActiveDocument.Sections(2).Range
    r.Select

    ' search again, and if found
    ' add another bookmark, and
    ' hyperlink back to first one
    With Selection.Find
    .Text = "(in parenthesis)"
    .Execute
    If .Found = True Then
    With ActiveDocument.Bookmarks
    .Add Range:=Selection.Range, Name:="myHyper2"
    End With
    ChangeFileOpenDirectory "C:\Gerry\"
    ActiveDocument.Hyperlinks.Add _
    Anchor:=Selection.Range, Address:="", _
    SubAddress:="myHyper1", ScreenTip:="", _
    TextToDisplay:="(in parenthesis)"
    End If
    End With
    Set r = Nothing
    End Sub[/vba]

  3. #3
    VBAX Contributor
    Joined
    Oct 2004
    Location
    Cardiff
    Posts
    120
    Location
    Hi Gerry,

    Thanks very much for your answer. Now I'll attempt to clear the fuzziness by answering your questions:

    Quote Originally Posted by fumei
    Say you have 14 occurrences of your text in Section 1- whatever it is - and for the sake of argument, 14 in Section 2. What do you want to happen? Occurrence 1 Section 1 links with occurrence 1 Section 2, occurrence 2 Section 1 links with occurrence 2 Section 2?
    Yes, that's right. Occurrence 1 Section 1 links with occurrence 1 Section 2, occurrence 2 Section 1 links with occurrence 2 Section 2 an so on.

    Further, though you don't ask I see in your code the expression:

    .Text = "(in parenthesis)"

    Are you saying here that I would have to specify the text to look for? Or does Word recognise that code as any text in parentheses?

    Also, I'm using Word97 on WinNT 4.0. I don't know whether that's the reason behind a compile error I get when I run you code. It says "Named objects not found" and highlight the ScreenTips bit at the end of the code, and when I take that bit of code out to see what happens, I get the same error on the "TextToDisplay" line immediately after it.

    I hope my question is has better definition now.

    Thanks for everything so far

    Damo
    You cannot solve all the world's major problems using only potatoes. :- Adams

    http://www.vbaexpress.com

  4. #4
    VBAX Contributor
    Joined
    Oct 2004
    Location
    Cardiff
    Posts
    120
    Location
    Any ideas?
    You cannot solve all the world's major problems using only potatoes. :- Adams

    http://www.vbaexpress.com

  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Damo,

    I'm fairly sure Gerry means for you to put your own search criteria in. Word will find any (non-specific) text in parentheses if you want it to ..
    [VBA]
    :
    :
    With Selection.Find
    .MatchWildcards = True
    .Text = "\(*\)"
    .Execute
    :
    :[/VBA]

    I don't have Word 97 in front of me at the moment but I suspect ScreenTip is newer than that. TextToDisplay, however, should work - you haven't also deleted the continuation, have you?
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6
    VBAX Contributor
    Joined
    Oct 2004
    Location
    Cardiff
    Posts
    120
    Location
    Quote Originally Posted by TonyJollans
    ...you haven't also deleted the continuation, have you?
    Sorry Tony,

    How do you mean?

    Damo
    You cannot solve all the world's major problems using only potatoes. :- Adams

    http://www.vbaexpress.com

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    1. Yes of course. The "in parenthesis" was just a sample, as that was what you put in. The search criteria can be adjusted for whatever you need.

    2. I do not have 97, so yes, if you are getting errors on parameters....take them out.

  8. #8
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Damo,

    Just had a quick check and TextToDisplay is not valid in 97 - Anchor, Address and SubAddress are all you can have.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  9. #9
    VBAX Contributor
    Joined
    Oct 2004
    Location
    Cardiff
    Posts
    120
    Location
    Quote Originally Posted by TonyJollans
    Hi Damo,

    Just had a quick check and TextToDisplay is not valid in 97 - Anchor, Address and SubAddress are all you can have.


    Thanks,

    Does this mean I'm stuffed?

    Damo
    You cannot solve all the world's major problems using only potatoes. :- Adams

    http://www.vbaexpress.com

Posting Permissions

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