Frank
2/29/2012 12:21:00 PM
The following VBA macro search through a given Word document and
removes text blocks that start with "row size:" and ends with
"Preceding task".
Initially the macro counts number of text occurences. During the
search and removal operations, a Userform show a status for number of
performed operations.
The problem is that number of performed operations does not equal
number of total search text occurences.
Thanks for any suggestions.
Regards
Frank
___________________________
Sub EditMyDoc()
Dim hitsTotally, hitsSoFar as Integer
Application.WindowState = wdWindowStateMinimize
UserForm1.Show
Application.ScreenUpdating = False
hitsTotally = 0
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "row size:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
Do While .Execute
Selection.Find.ClearFormatting
With Selection.Find
.Text = "row size:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
End With
Selection.Find.Execute
hitsTotally = hitsTotally + 1
Loop
End With
hitsSoFar = 0
Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Text = "row size:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
Do While .Execute
Selection.Extend
Selection.Find.ClearFormatting
With Selection.Find
.Text = "Preceding task"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Selection.Find.Execute
hitsSoFar = hitsSoFar + 1
UserForm1.Label2.Caption = "Editing " & hitsSoFar & " of " &
hitsTotally & " occurrences."
UserForm1.Repaint
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.TypeBackspace
Selection.HomeKey Unit:=wdStory
With Selection.Find
.Text = "row size:"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
End With
Loop
End With
Application.ScreenUpdating = True
Selection.HomeKey Unit:=wdStory
UserForm1.hide
Application.WindowState = wdWindowStateMaximize
End Sub