0

I'm working on a macro that parses a document and modifies style when needed. So far, one of my sub uses Find & Execute with a loop to go through all paragraph with a defined Style. It worked well enough and made it easy to know how many times an modification is made. However, it appears that .Execute Replace:=wdReplaceAll is far more efficient, but doesn't return this latter information in VBA, even though it is displayed when used directly in Word (with Ctrl + H).

How can I bypass this issue to count the number of replacements?

Thanks a lot in advance.

braX
  • 11,506
  • 5
  • 20
  • 33
Ezor
  • 135
  • 2
  • 18

2 Answers2

1

You could do this with a combination of Word's built in find and replace and a search and replace using the regex library (Microsoft VBScript Regular Expressions 5.5).

The VBScript regular expressions cannot search for styles, only text but can provide the number of matches found.

Thus you first do a search and replace for the paragraph marker of the style in which you are interested (^p + style). You replace the paragraph marker with an amended paragraph marker such as '###^p' being careful to replace with the same style.

You then use the regex search and replace to find the paragraph marker modifier and replace it with nothing, thus restoring the original text. The regex has a method .Matches.Count which will give you the number of replacements of ### that were made.

You may find the following stack overflow link of help

How to Use/Enable (RegExp object) Regular Expression using VBA (MACRO) in word

freeflow
  • 4,129
  • 3
  • 10
  • 18
  • Thanks; I'll have a look at this solution. However, my macro is supposed to work everywhere. Therefore I'd like that everything fit into a single module for my users aren't supposed to make to many things (like enable regular expression and so on) to use it. So far, I just provided a .txt document with the code in it, which was quite easy for them to copy/paste. I like the idea of temporary pattern used as mark to count replacement. May I use `ReplaceAll` with text in vba to do so (I'll deal with Style separattly if needed) ? – Ezor Oct 17 '18 at 08:14
  • 1
    If they are cutting and pasting the code then they are already in the IDE. Its a simple job to go Tools.References and to then scroll down to 'Microsoft VBScript Regular Expressions 5.5' and to ensure that the check box is ticked. The necessary dll is part of the office or windows installation. – freeflow Oct 17 '18 at 10:25
  • "_The necessary dll is part of the office or windows installation._" oh, that's interesting ! I read you link through and find out that I can use this `RegExp` in a module as other object. I'll try this in my code and let you know. thanks again for your help – Ezor Oct 17 '18 at 12:55
0

Try something based on:

Sub Demo()
Application.ScreenUpdating = False
Dim i As Long
With ActiveDocument.Range
  With .Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = InputBox("What is the Text to Find")
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    .Execute
  End With
  Do While .Find.Found
    i = i + 1
    .Collapse wdCollapseEnd
    .Find.Execute
  Loop
End With
Application.ScreenUpdating = True
MsgBox i & " instances found."
End Sub

The above code doesn't actually replace anything - it simply counts the instances of what's found.

macropod
  • 12,757
  • 2
  • 9
  • 21
  • Actually, I was trying to not using loops since it's slower than `replaceAll`. but maybe a counter without any modification could work. – Ezor Oct 19 '18 at 09:08
  • 1
    I *can* be done without a loop, but not without modifying the document. Basically, you'd get the character count do a Find/Replace with different-length Find & Replace expressions, deduct the current get the character count from the previous one and divide the result by the difference in the Find & Replace expression lengths to get the desired count. If wanted, you could then undo the Find/Replace. – macropod Oct 19 '18 at 11:36
  • @macropod do you possibly know how to include the counter in the replaced text? i.e. if 5 instances of "name" exist, replace them with name_1, name_2, etc. – El_1988 Nov 12 '20 at 02:54
  • Yes. That's hardly rocket science - the posted code even has the counter. All you need do is invest a trivial effort... – macropod Nov 12 '20 at 04:41