MS Word 2003 Macro, Styles

K

KenDude

I am relatively new to Word macros. I have created a macro that finds a
characters, selects severl characters, then changes the style of the
selection.
I loop through this routine until "Find.Found = False" When I run the macro
it will only change every other selection. If I step through the macro it
will change every selection.
I am sure that this is an MS bug, but maybe there is a work around.

If I put a break at line 15, then it doesn't skip every other selection.
If I put a break at line 16, then it does. The selection is executed, but
the style doesn't get changed.
I have also tried to put in a delay with a 1 to 100000000 for...next loop,
that doesn't help.

Here is the code, with added line numbers for clarity:

Sub Item_Number_Style()

1 ActiveDocument.Characters(1).Select
2 Selection.MoveLeft unit:=wdCharacter, Count:=1
3 Selection.Find.ClearFormatting

4 With Selection.Find
5 .Text = "["
6 .Replacement.Text = ""
7 .Forward = True
8 .Wrap = wdFindStop
9 End With

10 Selection.Find.Execute

11 Do While Selection.Find.Found = True

12 Do Until Selection.Characters.Last.Text = "]"
13 Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
14 Loop
15 Selection.Style = ActiveDocument.Styles(wdStyleHeading2)
16 Selection.MoveRight unit:=wdCharacter, Count:=1
17 Selection.Find.Execute

18 Loop
19 Selection.Find.ClearFormatting

End Sub
 
J

Jay Freedman

Hi Ken,

I haven't tried to analyze why your macro has the problem you
describe, because even if it worked correctly it would be a very
inefficient way of programming.

In fact, you don't need a macro at all unless this is something that
you repeat on many documents. To do it just once, open the Replace
dialog, click the More button to expand it, and check the Use
Wildcards option. In the Find What box, enter the code

\[[!^13]@\]

In the Replace With box, enter the code

^&

and then click the Format button, choose Style, and select the Heading
2 style from the popup. Finally, click the Replace All button, and
you're done.

If you do want a macro, use this one, which does the same thing:

Sub Item_Number_Style_2()
Dim oRg As Range
Set oRg = ActiveDocument.Range

With oRg.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Style = ActiveDocument.Styles(wdStyleHeading2)
.Text = "\[[!^13]@\]"
.Replacement.Text = "^&"
.Wrap = wdFindContinue
.Format = True
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
End Sub

Using a Range object instead of the Selection object has a number of
advantages, one being that Word doesn't have to scroll the screen and
move the cursor around, so it's quicker and doesn't lose the user's
place in the document.

See http://www.gmayor.com/replace_using_wildcards.htm and
http://www.word.mvps.org/FAQs/MacrosVBA/ModifyRecordedMacro.htm for
explanations and some additional tips.

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


I am relatively new to Word macros. I have created a macro that finds a
characters, selects severl characters, then changes the style of the
selection.
I loop through this routine until "Find.Found = False" When I run the macro
it will only change every other selection. If I step through the macro it
will change every selection.
I am sure that this is an MS bug, but maybe there is a work around.

If I put a break at line 15, then it doesn't skip every other selection.
If I put a break at line 16, then it does. The selection is executed, but
the style doesn't get changed.
I have also tried to put in a delay with a 1 to 100000000 for...next loop,
that doesn't help.

Here is the code, with added line numbers for clarity:

Sub Item_Number_Style()

1 ActiveDocument.Characters(1).Select
2 Selection.MoveLeft unit:=wdCharacter, Count:=1
3 Selection.Find.ClearFormatting

4 With Selection.Find
5 .Text = "["
6 .Replacement.Text = ""
7 .Forward = True
8 .Wrap = wdFindStop
9 End With

10 Selection.Find.Execute

11 Do While Selection.Find.Found = True

12 Do Until Selection.Characters.Last.Text = "]"
13 Selection.MoveRight unit:=wdCharacter, Count:=1, Extend:=wdExtend
14 Loop
15 Selection.Style = ActiveDocument.Styles(wdStyleHeading2)
16 Selection.MoveRight unit:=wdCharacter, Count:=1
17 Selection.Find.Execute

18 Loop
19 Selection.Find.ClearFormatting

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