Macro for replacement processing

I

ivanov.ivaylo

Hi all,

I need further help with a macro. I reached the place where I have to
do this search and replace action:


Find: (selected phrase, e.g. "green piano")
Replace: <a href="bword://green piano">green piano</a>


That is to say, I'll select sertain phrases from the text and when I
run the macro the selected phrase should be tagged like this: <a
href="bword://XYZ">XYZ</a>


XYZ is any phrase I choose to select. I'll assign the macro to a button
for quick work.


Ivaylo
 
D

Doug Robbins

Dim findstr As String
findstr = Selection.Text
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findtext:=findstr, MatchWildcards:=False,
Wrap:=wdFindStop, Forward:=True) = True
Selection.Text = "<a href=" & Chr(34) & "bword://" & findstr &
Chr(34) & ">" & findstr & "</a>"
Selection.Collapse wdCollapseEnd
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
I

ivanov.ivaylo

Thank you very much Doug!

It works like hell!

Regards,

Ivaylo

Here it is the whole macro, for anybody interested:

Sub HyperLinks()
Dim findstr As String
findstr = Selection.Text
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findtext:=findstr, MatchWildcards:=False,
Wrap:=wdFindStop, Forward:=True) = True
Selection.Text = "<a href=" & Chr(34) & "bword://" & findstr &
Chr(34) & ">" & findstr & "</a>"
Selection.Collapse wdCollapseEnd
Loop
End With
End Sub
 
I

ivanov.ivaylo

Hi Doug again!

If I want to use your code without a looping cycle, whuch line I should
delete. I want to find individual instances.

Here is the macro I use:

Sub HyperLinks()
Dim findstr As String
findstr = Selection.Text
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(findtext:=findstr, MatchWildcards:=False,
Wrap:=wdFindStop, Forward:=True) = True
Selection.Text = "<a href=" & Chr(34) & "bword://" & findstr &
Chr(34) & ">" & findstr & "</a>"
Selection.Collapse wdCollapseEnd
Loop
End With
End Sub


Many thanks

Ivaylo
 
D

Doug Robbins - Word MVP

Use:

With Selection
.Text = "<a href=" & Chr(34) & "bword://" & .Text & Chr(34) & ">" & .Text
& "</a>"
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
I

ivanov.ivaylo

Thank you again Doug,

Here it is the moditied version of the macro, for reference of the
community:

Sub HyperLinks()

Dim findstr As String
findstr = Selection.Text
Selection.Find.ClearFormatting
With Selection
.Text = "<a href=" & Chr(34) & "bword://" & findstr & Chr(34) & ">" &
findstr & "</a>"
End With
End Sub
 

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