Hello guys,
I searched the Internet in the past and I found the following piece of code.
Sub FindAndReplaceFirstStoryOfEachType(OldText As String, NewText As String)
Dim rngStory As Range
For Each rngStory In ActiveDocument.StoryRanges
With rngStory.Find
.Text = OldText
.Replacement.Text = NewText
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
Next rngStory
End Sub
I do not understand this Range thing (despite my experience in VBA in Excel), however this code does work.
I need an upgrade to that code, to convert the Sub (the procedure) into a Function that would return the number of replacements.
This code has proved to be excellent in replacing two consecutive spaces with one but ... ... here comes the big Bee Yu Tee. What about having three consecutive spaces or four?
I could make a loop and run it many times e.g. 12 times. It would be rather unlikely to have more than 12 consecutive spaces in a document. But that sounds stupid and also increases the time required.
If I could get the number of replacement I could run it in a repeat-until loop and run it until the number of replacements becomes zero.
Any idea?
Bookmarks