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
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