Replace with hyperlinked words

  • Thread starter 0-0 Wai Wai ^-^
  • Start date
0

0-0 Wai Wai ^-^

I would like to perform the following:
In my passage, I would like to hyperlink the word "business".
However the words are spread in anywhere, and the words repeat for many and many
times.

To save time, I would like to use "replace" function.
Unfortunately there is no way for me to choose to replace them with hyperlinked
ones.

Does anyone think of any workaround to hyperlink all words "business"
effectively?
 
C

Chip Orange

below is some macro code I'm using to hyperlink a particular word; I'm using
a range which isn't my entire document, but hopefully you'll be able to
change it to work with your entire document, and change the word hyperlinked
to be "business":


Set objTempRange = objCurSectionRange.Duplicate
With objTempRange.Find
.ClearFormatting
.Text = "Recommendation:"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

Do While objTempRange.Find.Execute
If objTempRange.Hyperlinks.Count = 0 Then
' what we found is not a hyperlink already
Set objNewLink = ActiveDocument.Hyperlinks.Add _
(Anchor:=objTempRange, Address:= _
strToFile, TextToDisplay:="Recommendation:")
' creating the hyperlink unselected the found text
' reset our search to begin at the end of the new hyperlink
objTempRange.Start = objNewLink.Range.End
Else
' because the found text is still selected, it needs to be collapsed.
objTempRange.Collapse wdCollapseEnd
objTempRange.MoveStart Unit:=wdCharacter, Count:=1
End If

' prepare for continuing the search by
' redefining the end of objTempRange to be that of the section
again.
objTempRange.End = objCurSectionRange.End
Loop
 
0

0-0 Wai Wai ^-^

How to use the macro code?
I'm new at that. (I know how to activate it, but I can't understand what the
code really means)

And when I try to create a macro and copy your codes into it, and run it, it
gets an error message of "424.

What's wrong?
 
J

Jonathan West

0-0 Wai Wai ^-^ said:
How to use the macro code?
I'm new at that. (I know how to activate it, but I can't understand what the
code really means)

And when I try to create a macro and copy your codes into it, and run it, it
gets an error message of "424.

What's wrong?

You need to paste the code in between a pair of lines that are like this


Sub MyMacro()

End Sub

For more on using macro code that is provided in newsgroup answers, take a
look at this article

What do I do with macros sent to me by other newsgroup readers to help me
out?
http://word.mvps.org/FAQs/MacrosVBA/CreateAMacro.htm
 
0

0-0 Wai Wai ^-^

I don't think it is a problem of installing/creating.
I think it is something to do with the code inside.

Please see my full macro code.

Sub Replace_with_hyperlinked_texts()
Set objTempRange = objCurSectionRange.Duplicate
With objTempRange.Find
.ClearFormatting
.Text = "Recommendation:"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

Do While objTempRange.Find.Execute
If objTempRange.Hyperlinks.Count = 0 Then
' what we found is not a hyperlink already
Set objNewLink = ActiveDocument.Hyperlinks.Add _
(Anchor:=objTempRange, Address:= _
strToFile, TextToDisplay:="Recommendation:")
' creating the hyperlink unselected the found text
' reset our search to begin at the end of the new hyperlink
objTempRange.Start = objNewLink.Range.End
Else
' because the found text is still selected, it needs to be collapsed.
objTempRange.Collapse wdCollapseEnd
objTempRange.MoveStart Unit:=wdCharacter, Count:=1
End If

' prepare for continuing the search by
' redefining the end of objTempRange to be that of the section again.
objTempRange.End = objCurSectionRange.End
Loop


' Replace_with_hyperlinked_texts Macro
' Macro recorded on XX., 2004 by XX
'
End Sub
 
J

Jonathan West

The reason you have a problem is that you have a variable called
objTempRange which you haven't declared properly.

The the following line of code immeduately after the sub statement

Dom objTempRange as Range

To get an earlier warning of such problems, I would recommend you tae a look
at the following article

Why variables should be declared properly
http://word.mvps.org/FAQs/MacrosVBA/DeclareVariables.htm
 
C

Chip Orange

There was a range variable that my example code did not initialize, but
which does need to be initialized at the start (to that of the entire
document if nothing else).
 
C

Chip Orange

You may want to pick up some books on VBA programming; also, you may want to
check out the news group microsoft.public.word.vba.beginners

hth,

Chip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top