Do I need all these lines for the macro?

A

angellijah

When I create a macro and then look at the VBA, it has a lot of lines that I
don't think are necessary. Here is what I got for a find macro:

With Selection.Find
.Text = "run date"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

Do I have to have all of those lines?
 
J

Jay Freedman

angellijah said:
When I create a macro and then look at the VBA, it has a lot of lines
that I don't think are necessary. Here is what I got for a find
macro:

With Selection.Find
.Text = "run date"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With

Do I have to have all of those lines?

Maybe, but probably not. :) First read
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm for some
background.

The "maybe" part concerns the way the values of the properties in
Selection.Find are "sticky". If the user or your macro or some other macro
has run any kind of Find or Replace operation in the current session before
you start, any nondefault value that were set before will still be there.
For example, if a previous Find had set .Wrap = wdFindStop, any your macro
doesn't mention the .Wrap property, then it will use wdFindStop instead of
the default wdFindContinue.

There are two things you can do about this:

- Leave all the default settings in your code, at least for the first Find
or Replace that you do. That would override any nondefault values left over
from who knows what.

Or...

- Instead of using Selection.Find, declare a Range object, set it to
ActiveDocument.Range, and use its .Find like this:

Dim myRange As Range
Set myRange = ActiveDocument.Range
With myRange.Find
.Text = "run date"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With

This has two advantages: First, everything in myRange.Find is automatically
set to default values when you declare it; there is no "stickiness" for a
Range. So you can confidently include only the properties that you want to
use. Second, the execution of this Find won't move the cursor the way
Selection.Find would, so screen redrawing isn't necessary and it runs a lot
faster.

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

angellijah

Wonderful, thank you so much!!

Jay Freedman said:
Maybe, but probably not. :) First read
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm for some
background.

The "maybe" part concerns the way the values of the properties in
Selection.Find are "sticky". If the user or your macro or some other macro
has run any kind of Find or Replace operation in the current session before
you start, any nondefault value that were set before will still be there.
For example, if a previous Find had set .Wrap = wdFindStop, any your macro
doesn't mention the .Wrap property, then it will use wdFindStop instead of
the default wdFindContinue.

There are two things you can do about this:

- Leave all the default settings in your code, at least for the first Find
or Replace that you do. That would override any nondefault values left over
from who knows what.

Or...

- Instead of using Selection.Find, declare a Range object, set it to
ActiveDocument.Range, and use its .Find like this:

Dim myRange As Range
Set myRange = ActiveDocument.Range
With myRange.Find
.Text = "run date"
.Replacement.Text = ""
.Execute Replace:=wdReplaceAll
End With

This has two advantages: First, everything in myRange.Find is automatically
set to default values when you declare it; there is no "stickiness" for a
Range. So you can confidently include only the properties that you want to
use. Second, the execution of this Find won't move the cursor the way
Selection.Find would, so screen redrawing isn't necessary and it runs a lot
faster.

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


.
 

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