macro question

H

Hamilton

I'm trying to start off the first command to change all single angle
brackets found, to double angle brackets, but debug is stopping at the
first line. Can someone tell me what I did wrong?

With Selection.Find
.Text = "<^&>"
.Replacement.Text = "<<^&>>"
.Forward = True
.Wrap = wdFindContinue
.Format = 0
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll

Using Word 2000 and Microsoft Visual Basic
 
G

Greg Maxey

Hamilton,

I don't know why it is stoping at the first line. Did you provide the first
line?

If I understand correctly, you are trying to make something like ....

Now <is> the <time> for <all> good <men> ... into
Now <<is>> the <<time>> for <<all>> good <<men>>

If so you could do a wildcard search:

Sub ScratchMacro()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "\<(*)\>"
.Replacement.Text = "<<\1>>"
.Forward = True
.Wrap = wdFindContinue
.Format = 0
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub

or two plain searches:

Sub ScratchMacro2()

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "<"
.Replacement.Text = "<<"
.Forward = True
.Wrap = wdFindContinue
.Format = 0
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With

With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ">"
.Replacement.Text = ">>"
.Forward = True
.Wrap = wdFindContinue
.Format = 0
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With

End Sub
 
H

Helmut Weber

Hi Hamilton,
not really.
But who cares of an explanation,
if there is a working solution:

Sub Test5001()
Selection.ExtendMode = False
Selection.HomeKey unit:=wdStory
ResetSearch
With Selection.Find
.Text = "\<"
.Replacement.Text = "<<"
.Forward = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
.Text = "\>"
.Replacement.Text = ">>"
.Forward = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
ResetSearch
End Sub

' ---
Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub

Greetings from Bavaria,
a region in Europe,
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
H

Helmut Weber

Hi Greg,
the thing is what happens if, from left to right,
there is not for each opening pointed bracket
a closing pointed bracket whithout any other opening pointed
bracket enterfering, like:

Now <is> <the <time> for <all> good <men>>

Therefore the result of .Text = "\<(*)\>"
might not meet the expections and therefore
called a failure.

Greetings from Bavaria,
a region in Europe,
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
G

Greg Maxey

Helmut,

True, but then there were no ocurrences of that possibility in my sample
string :)
 
J

Jay Freedman

Greg and Helmut have given you workarounds. For completeness, the
reason for the error is that ^& isn't a valid code in the .Text field.
It means "the found text" and it makes sense only in the
..Replacement.Text field.
 

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