Macros in VB in Word

A

airhockeycanada

Good day everyone!

I have created some macros in Microsoft Word v10 that search out certain
items in a document and adjust them in various ways.

Here is part of one of my macros as an example:

Selection.Find.ClearFormatting
With Selection.Find
.Text = "s0p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchKashida = False
.MatchDiacritics = False
.MatchAlefHamza = False
.MatchControl = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

The macro looks for the text 's0p' and removes it. The document contains
many of these and we need to remove them all. How do I have this loop to
continually search for new occurrences of 's0p' until no more remain and the
macro ends?

Thank you VERY much for any help you can offer.
 
J

Jay Freedman

Just before the End With statement, insert this line:

.Execute Replace:=wdReplaceAll

That's equivalent to clicking the Replace All button in the Replace dialog.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
A

airhockeycanada

Jay,

Thanks but, one piece of information I should have added. These character
strings in this case that I'm searching for are the common denominators in
all these things I'm searching out (e.g. @#$es0p&& and $###s0p@@@). In both
cases, I want the macro to run and delete these entire strings. But, to
search for them I search for s0p. So, what you proposed won't work. Is there
a suggestion for this?

I currently have the macro set up and it runs fine but, I only know how to
get it to do one occurrence and it stops. To delete more occurrences, I have
to run the macro again. What I want is for it to continue to loop and search
until it finds no further occurrences of 's0p'.

I appreciate your assistance.
 
J

Jay Freedman

Depending on exactly what (besides the "s0p") is common to the things
you're removing, you can probably use a wildcard search and still use
the Replace All.

Read http://www.gmayor.com/replace_using_wildcards.htm for an
explanation of how wildcards work.

For example, if every instance to be removed has four characters
before "s0p" and three characters after it, the search with

.Text = "????s0p???"
.MatchWildcards = True
' everything else the same as before
.Execute Replace:=wdReplaceAll

will remove all of the instances in one operation.

The technique of looping repeatedly should be avoided if at all
possible. If it turns out to be the only technique that works, though,
it can be done like this:

Selection.Find.ClearFormatting
With Selection.Find
.Text = "s0p"
' don't set .Replacement.Text, just find
.Forward = True
.Wrap = wdFindStop ' <= note change here
' other settings

While .Execute
' do something here to decide whether to delete
' and if so, how much
Wend
End With

The .Execute method returns True if it finds the item, or False if it
doesn't find any more. It's up to you to figure out the logic of the
"do something here".
 
A

airhockeycanada

Jay,

I had written the macro after your suggestion (I did use the looping
suggestion) and it worked. However, I tried to add something else to the
macro to add some more functionality and I messed it up. Now, trying to
replicate what I first did is seeming impossible. I'm trying to format an
item on each page of a document and this is the macro:

Sub StmtAddressAll()
'
' StmtAddressAll Macro
' Macro recorded 8/21/2009 by S Ross
'
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Investment Statement"
.MatchCase = True
.Forward = True
.Wrap = wdFindStop
Selection.Find.Execute
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.HomeKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveUp Unit:=wdLine, Count:=7, Extend:=wdExtend
Selection.TypeBackspace
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Selection.MoveDown Unit:=wdLine, Count:=7, Extend:=wdExtend
With Selection.Font
.Name = "Courier New"
.Size = 12
.Bold = False
.Italic = False
.Underline = wdUnderlineNone
.UnderlineColor = wdColorAutomatic
.StrikeThrough = False
.DoubleStrikeThrough = False
.Outline = False
.Emboss = False
.Shadow = False
.Hidden = False
.SmallCaps = False
.AllCaps = False
.Color = wdColorAutomatic
.Engrave = False
.Superscript = False
.Subscript = False
.Spacing = -0.6
.Scaling = 100
.Position = 0
.Kerning = 0
.Animation = wdAnimationNone
.SizeBi = 10
.NameBi = "Courier New"
.BoldBi = False
.ItalicBi = False
End With
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.TypeParagraph
End Sub

I keep getting an error. Anything you see wrong with it? Thanks.
 
G

Graham Mayor

Without attempting to discover what it is the macro is supposed to do, the
error message is attributable to a missing 'end with' line which presumably
goes after

With Selection.Find
.Text = "Investment Statement"
.MatchCase = True
.Forward = True
.Wrap = wdFindStop

If you want more help with it, you had better explain *exactly* what you are
trying to achieve.
--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
A

airhockeycanada

Graham,

The macro is designed to format the title and address portions of statements
that our system generates and I import into Word. It searches out the title
of each page and then eliminates lines so the title appears on the first line
of each page. Then, it formats the name and address by increasing the font.
It is designed to seek out page titles and then format the title and name and
address for each until there are no more titles in the document. I hope this
helps.
 
G

Graham Mayor

Not really, without seeing the document. Can you send before and after
samples to the link on the home page of my web site?

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 

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