Link to home
Start Free TrialLog in
Avatar of nick_harambee
nick_harambee

asked on

i need a macro to create automatic hyperlinks in microsoft word that refers to a table with words and associated hyperlinks

hello,

r_rajesh kindly wrote me a macro that will automatically convert specific text to specific hyperlinks in microsoft word 2002.  this works fine, but i would like to make it a bit more user friendly.  at the moment i have to edit the macro each time i want to add new words to the list.

what i have in mind, is setting up a table (like a concordance table) in word, with 2 columns.  in the first column would be the words, and in the second the hyperlinks that i would like the words to point to.  the macro would then look at this table each time i run it to see what the current list is, and make the appropriate hyperlinks

thanks,

nick.
Avatar of GrahamSkan
GrahamSkan
Flag of United Kingdom of Great Britain and Northern Ireland image

This needs a word document with a two-column table to be open at the same time as the document that you are working on. The first column as the word(s) to be set to the hyperlink which should be in the second column.
The word(s) should be selected by the user.

Sub InsertHyper()
Dim docWithTable As Document
Dim SelectText As String
Dim tbl As Table
Dim rw As Row

SelectText = AllTrim(Selection.Text)
Set docWithTable = Documents("HyperLinks.Doc")
Set tbl = docWithTable.Tables(1)
For Each rw In tbl.Rows
    If UCase$(AllTrim(rw.Cells(1).Range.Text)) = UCase$(SelectText) Then
        ActiveDocument.Hyperlinks.Add Selection.Range, rw.Cells(2).Range.Text, , SelectText
        Exit For
    End If
Next rw
End Sub

'Removes leading and trailing tabs, spaces, paragraph marks, etc
Function AllTrim(Text As String) As String
Dim p As Integer
Dim q As Integer
p = 1
Do While Asc(Mid$(Text, p)) < 33
    p = p + 1
Loop
q = Len(Text)
Do While Asc(Mid$(Text, q)) < 33
    q = q - 1
Loop
AllTrim = Mid$(Text, p, q - p + 1)
End Function
Avatar of nick_harambee
nick_harambee

ASKER

thanks graham,

i have installed your macro and set up the second document with the table in it and kept it open.  when i run the macro though i get the following message:

Run Time Error 5. Invalid procedure call or argument.

In the table i have set up there is no header row.  i have written the word to be hyperlinked on the left, and the path for the hyperlink on the right as C:\Documents and Settings\nick\My Documents....  Do I need to name the document with the table in anything in particular?

when you say 'the words need to be selected by the user', what exactly do you mean?

Finally, ideally I would like a macro to run without having to have the second document with the table in open.  is this possible?

thanks,

nick.
Hi, Nick! (good deal!)

I think Graham means that you need to select the text on which you want to run the code.

In other words, you'd hit Ctrl+A if you want to run it on the entire "active document".
Hi Nick,

The procedures are not bulletproof, because I don't know enought about your situation.
Where does it fail and what are the values of the variables at the point of failure?

As designed, the particular word should be in the left hand column - column1, with the hyperlink in column 2. No header row is expected. I have presumed that the document name is "HyperLinks.Doc". If is is already open, the path doesn't matter to the routine as it stands.

'the words need to be selected by the user'.

It wasn't clear how the macro would decide which words in the document are to be hyperlinked. The way that this is intended to work is for the user to select (highlight) the word or phrase in question. If you had some other method in mind, then we could work on that.

You must have the table somewhere and if it is on another document, the document must be open. You could hold it on the same document, or you could have a spreadsheet or an Access table or a text file.

If necessary, the opening of the table document could be done in the code:

e.g.
Set docWithTable = Documents.open("C:\Documents and Settings\nick\My Documents\HyperLinks.Doc")
Comments crossed again, Dreamboat.

No, I have only written it to work on the selected phrases, so ctrl-a would not do it.

Nick if you need it to search through the document or selection for every occurence of the words/phrases, then let me know. It is not written to do that at the moment.
Yes, Graham. As I understood Nick's request, he wants it to find any of the words in column A, and replace them with the hyperlink in column B. Presumably, Nick does this a LOT and it's safe to run the macro on ALL text in the currently active document.

Also, presumably, Nick would prefer not to SEE the hyperlinks.doc file open, but would certainly want it to close upon completion if it DOES open "in sight"..

You can find his original request here, which was fulfilled, so I encouraged him to *re-ask* the question with specs:
https://www.experts-exchange.com/questions/21136695/how-to-create-automatic-hyperlinks-in-microsoft-word-2002.html
yes, dreamboat describes what i want accurately.  it would be safe to run the macro on all text in the document, and ideally i don't want to have to open the hyperlinks.doc file.

thanks

nick.
ASKER CERTIFIED SOLUTION
Avatar of R_Rajesh
R_Rajesh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Okay. I tested in 2002.

On these lines:

ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, _
Address:=Left(myLinks(i).sHyper, Len(myLinks(i).sHyper) - 2)


I get this error:
Bad Parameter

HOWEVER....it DOES work.
All links become converted.
yes rajesh's new macro is working for me as well, and i am not getting an error, but it was pretty slow in picking up just one link.  i will try both of rajesh's macros tomorrow with several links and let you know how i get on.

thanks

nick.
no problem, nick

DB: thanks very much for testing it. my guess is, at the time of the error myLinks(i).sHyper becomes null or something (when error occurs and the execution breaks, hovering the mouse over myLinks(i).sHyper will display the data it contains..
thanks rajesh, for both your macros.  they both work fine.  for obvious reasons, the one that points to a table is a little slow, whereas the macro that contains the info about hyperlinks to make is almost instantaneous.  so i will have to work out if user friendliness or speed is more important to me here.  anyway, thanks again for your help,

nick.