PDA

View Full Version : Code to put hyperlinks in automatically



The Tamer
02-22-2005, 10:09 AM
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. :beg:

Damo

fumei
02-22-2005, 11:37 AM
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:
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

The Tamer
02-23-2005, 01:58 AM
Hi Gerry,

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


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

The Tamer
02-24-2005, 02:12 AM
Any ideas?

TonyJollans
02-24-2005, 08:17 AM
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 ..

:
:
With Selection.Find
.MatchWildcards = True
.Text = "\(*\)"
.Execute
:
:

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?

The Tamer
02-24-2005, 08:23 AM
...you haven't also deleted the continuation, have you?Sorry Tony,

How do you mean?

Damo

fumei
02-24-2005, 08:24 AM
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.

TonyJollans
02-24-2005, 08:36 AM
Hi Damo,

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

The Tamer
02-25-2005, 05:57 AM
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