Qst: Find text Replace w/ hyperlink

S

sprungli

Is it possible to insert a hyperlink object instead of a text in a Word
Find-Replace? I found the code below in the VBA help. My goal is to have a
hyperlink which TextToDisplay property (let say http://localhost/MyApp or
C:\Temp\MyApp.exe) will appear in place of the myRange.Find.Replacement.Text
value.



Sub m1()



Set myRange = ActiveDocument.Range(Start:=0, End:=0)



With myRange.Find

.ClearFormatting

.Text = "Some text to find"



With .Replacement

.ClearFormatting

.Text = "Some replacement text"

' ? add Hyperlink instead of text

.Font.Bold = True

End With



.Execute Replace:=wdReplaceAll, _

Format:=True, MatchCase:=True, _

MatchWholeWord:=True

End With



End Sub





Thanks in advance for any suggestions.
 
C

Chad DeMeyer

I think you would need to do something like this:

With Selection.Find
'set find options here
.Execute
Do While Selection.Find.Found 'if .Found = True, then selection
has moved to where the Find.Text was found
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range,
Address:=[...], TextToDisplay:=[...]
.Execute
Loop
End With

Regards,
Chad DeMeyer
 
P

Peter Hewett

Hi sprungli

You can't do it quite like that. CHeck out the following example:

Sub FindAndReplaceWithHypertext()
Dim rngReplace As Word.Range
Dim rngFound As Word.Range

Set rngReplace = ActiveDocument.Content
With rngReplace.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "some text"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False

' Find all occurrences in the document
Do While .Execute

' Create and use a totally independent range object
Set rngFound = rngReplace.Duplicate

' Replace sought text with hyperlink
rngFound.Text = vbNullString
ActiveDocument.Hyperlinks.Add rngFound, "www.microsoft.com", _
TextToDisplay:="Text displayed in the document"

' Resume the search after the text that we just found
rngReplace.Collapse wdCollapseEnd
Loop
End With
End Sub

This replaces *all* occurrences of the sought text in the document with a hyperlink.

HTH + Cheers - Peter
 
S

sprungli

Good shot Chad!

This is exactly what I needed. Clean, short code. I will use your version
since it saves one step in the replacement, compared to Peter Hewett's
version (which also works fine, thank you Peter!) Another advantage over his
code: original formatting is automatically preserved in the TextToDisplay of
the hyperlink I guess this is because you use the Selection directly.

May I ask one more question: suppose the Address property of the Hyperlink
is set to some local path, e.g. C:\Temp\MyApp.exe; can I then pass
parameters to the MyApp.exe? For example, if the requested resource were a
Web application, the Address property would be set to something like
http://localhost/MyApp?param1=value1. Is there something similar for local
applications? I mostly work on the server side, so sorry if my question
sounds weird...

Many thanks and best regards,

S.


---
Chad DeMeyer said:
I think you would need to do something like this:

With Selection.Find
'set find options here
.Execute
Do While Selection.Find.Found 'if .Found = True, then selection
has moved to where the Find.Text was found
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range,
Address:=[...], TextToDisplay:=[...]
.Execute
Loop
End With

Regards,
Chad DeMeyer


sprungli said:
Is it possible to insert a hyperlink object instead of a text in a Word
Find-Replace? I found the code below in the VBA help. My goal is to have a
hyperlink which TextToDisplay property (let say http://localhost/MyApp or
C:\Temp\MyApp.exe) will appear in place of the myRange.Find.Replacement.Text
value.



Sub m1()



Set myRange = ActiveDocument.Range(Start:=0, End:=0)



With myRange.Find

.ClearFormatting

.Text = "Some text to find"



With .Replacement

.ClearFormatting

.Text = "Some replacement text"

' ? add Hyperlink instead of text

.Font.Bold = True

End With



.Execute Replace:=wdReplaceAll, _

Format:=True, MatchCase:=True, _

MatchWholeWord:=True

End With



End Sub





Thanks in advance for any suggestions.
 
S

sprungli

Thank very much you for your help Peter,

your code works fine without a single change! I will use Chad DeMeyer's
version though, since it saves one step in the replacement, and also keeps
the original formatting automatically.

Best regards,

S.


---
 
C

Chad DeMeyer

You would need to check the documentation for that particular application
and see if there are any command line switches that can be used (unless
you're the developer, in which case you already know!). Whether you can
include these in a hyperlink I don't know because I've never tried it.
However, if it doesn't work, you could use a MACROBUTTON field which runs a
macro that creates an ActiveX shell object (e.g., Set oShell =
CreateObject("WScript.Shell")) and then use the Run method of the Shell
object to start your application with the command line switches, exactly as
you would from the Start menu's Run prompt. Then you could include
"Options.ButtonFieldClicks = 1" in an AutoOpen macro so that the MACROBUTTON
works off a single click like a hyperlink, and you could even format the
field to look like a hyperlink.

Regards,
Chad DeMeyer


sprungli said:
Good shot Chad!

This is exactly what I needed. Clean, short code. I will use your version
since it saves one step in the replacement, compared to Peter Hewett's
version (which also works fine, thank you Peter!) Another advantage over his
code: original formatting is automatically preserved in the TextToDisplay of
the hyperlink I guess this is because you use the Selection directly.

May I ask one more question: suppose the Address property of the Hyperlink
is set to some local path, e.g. C:\Temp\MyApp.exe; can I then pass
parameters to the MyApp.exe? For example, if the requested resource were a
Web application, the Address property would be set to something like
http://localhost/MyApp?param1=value1. Is there something similar for local
applications? I mostly work on the server side, so sorry if my question
sounds weird...

Many thanks and best regards,

S.


---
Chad DeMeyer said:
I think you would need to do something like this:

With Selection.Find
'set find options here
.Execute
Do While Selection.Find.Found 'if .Found = True, then selection
has moved to where the Find.Text was found
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range,
Address:=[...], TextToDisplay:=[...]
.Execute
Loop
End With

Regards,
Chad DeMeyer


sprungli said:
Is it possible to insert a hyperlink object instead of a text in a Word
Find-Replace? I found the code below in the VBA help. My goal is to
have
 
S

sprungli

Thank you again Chad,

your help was much appreciated! The suggestion about the MACROBUTTON worked
fine. Just one remark: no need to create a COM object, there is a global
Shell function to use directly, e.g.

Dim RunningProcessID As Long
' Specifying 1 as the second argument opens the application in normal
size and gives it the focus.
'RunningProcessID = Shell("C:\WINNT\system32 \CALC.EXE <pass params here
if expected>", 1)

Best,

S.

---
Chad DeMeyer said:
You would need to check the documentation for that particular application
and see if there are any command line switches that can be used (unless
you're the developer, in which case you already know!). Whether you can
include these in a hyperlink I don't know because I've never tried it.
However, if it doesn't work, you could use a MACROBUTTON field which runs a
macro that creates an ActiveX shell object (e.g., Set oShell =
CreateObject("WScript.Shell")) and then use the Run method of the Shell
object to start your application with the command line switches, exactly as
you would from the Start menu's Run prompt. Then you could include
"Options.ButtonFieldClicks = 1" in an AutoOpen macro so that the MACROBUTTON
works off a single click like a hyperlink, and you could even format the
field to look like a hyperlink.

Regards,
Chad DeMeyer


sprungli said:
Good shot Chad!

This is exactly what I needed. Clean, short code. I will use your version
since it saves one step in the replacement, compared to Peter Hewett's
version (which also works fine, thank you Peter!) Another advantage over his
code: original formatting is automatically preserved in the
TextToDisplay
of
the hyperlink I guess this is because you use the Selection directly.

May I ask one more question: suppose the Address property of the Hyperlink
is set to some local path, e.g. C:\Temp\MyApp.exe; can I then pass
parameters to the MyApp.exe? For example, if the requested resource were a
Web application, the Address property would be set to something like
http://localhost/MyApp?param1=value1. Is there something similar for local
applications? I mostly work on the server side, so sorry if my question
sounds weird...

Many thanks and best regards,

S.


---
Chad DeMeyer said:
I think you would need to do something like this:

With Selection.Find
'set find options here
.Execute
Do While Selection.Find.Found 'if .Found = True, then selection
has moved to where the Find.Text was found
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range,
Address:=[...], TextToDisplay:=[...]
.Execute
Loop
End With

Regards,
Chad DeMeyer


Is it possible to insert a hyperlink object instead of a text in a Word
Find-Replace? I found the code below in the VBA help. My goal is to
have
a
hyperlink which TextToDisplay property (let say
http://localhost/MyApp
or
C:\Temp\MyApp.exe) will appear in place of the
myRange.Find.Replacement.Text
value.



Sub m1()



Set myRange = ActiveDocument.Range(Start:=0, End:=0)



With myRange.Find

.ClearFormatting

.Text = "Some text to find"



With .Replacement

.ClearFormatting

.Text = "Some replacement text"

' ? add Hyperlink instead of text

.Font.Bold = True

End With



.Execute Replace:=wdReplaceAll, _

Format:=True, MatchCase:=True, _

MatchWholeWord:=True

End With



End Sub





Thanks in advance for any suggestions.
 

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