Results 1 to 5 of 5
  1. #1
    Silver Lounger
    Join Date
    Jun 2001
    Location
    Morden, Surrey, United Kingdom
    Posts
    1,838
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Counting replacements (VBA W97)

    I am creating a template which needs to do repeated replacements of text, with the text to go in being decided by variables, hence:
    <pre>Selection.Find.Execute FindText:="A", ReplaceWith:=repA, _
    Wrap:=wdFindContinue, replace:=wdReplaceAll
    </pre>

    My problem is, I need to count how many replacements are made! Something along the lines of:
    <pre>If repA.Count < 2 Then MsgBox "There are less than two instances of 'A'", vbCritical
    </pre>

    Needless to say this line doesn't work - can anyone help, please? It's driving me nuts! <img src=/S/hairout.gif border=0 alt=hairout width=31 height=23>
    Beryl M


  2. #2
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Re: Counting replacements (VBA W97)

    This is somewhat long winded, so when someone posts a simpler solution we may both learn something.

    This will only replace the matched string once in any word, and if you have more than 1000 matches then it will stop after the first 1000. I also vaguely remember that it has a problem when the matched string exactly ends at the end of the document - but I haven't tested it recently so I may be wrong.

    <pre>Private Function ReplaceAndCount(OldText As String, NewText As String) As Integer

    Dim MatchCount As Integer

    With ActiveDocument.Content.Find
    .ClearFormatting
    .Text = OldText
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchAllWordForms = False
    .MatchSoundsLike = False
    .MatchWildcards = False

    MatchCount = 0
    Do While .Execute(ReplaceWith:=NewText) = True And MatchCount < 10000
    MatchCount = MatchCount + 1
    .Parent.Move Unit:=wdWord, Count:=1
    Loop
    End With

    ReplaceAndCount = MatchCount

    End Function

    </pre>


    StuartR

  3. #3
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Counting replacements (VBA W97)

    Another nice (and faster) way is described in the article <A target="_blank" HREF=http://www.mvps.org/word/FAQs/MacrosVBA/GetNoOfReplacements.htm>How to find out, using VBA, how many replacements Word made during a Find & Replace All</A> by Bart Verbeek and Dave Rado. It works by comparing the character count before and after the replacement.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16>Klaus

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts
    [quote name='Klaus Linke' post='105810' date='07-Jan-2002 22:14']Another nice (and faster) way ... by Bart Verbeek and Dave Rado.[/quote]
    I hesitate to contact a Microsoft MVP about this, but anyone else here may care to volunteer.
    I feel that the technique of using
    Code:
    	'Undo the replace
    	ActiveDocument.Undo
    is a Trap For Young Players, since the .Undo also undoes any changes made immediately prior to the procedure call.
    To get around this I instituted an
    Code:
    ActiveDocument.UndoClear
    at the head of the procedure(s).
    I then found that this sometimes had the effect of selecting the entire document text.
    A macro to type statistics was thereby able to report the count of words etc by obliterating the data source.
    Much to my mystification the next morning.
    I instituted
    Code:
    	Dim rngSelection As Range
    	Set rngSelection = Selection.Range ' preserve this to avoid the 'Undo"'s Select.All
    at the start and
    Code:
    	rngSelection.Select
    at the end.
    I am attaching a plagiarized version of Verbeek/Rado's excellent code, and meekly crawling back into my hole.
    Attached Files Attached Files
    • File Type: txt 1.txt (2.3 KB, 7 views)

  5. #5
    Plutonium Lounger
    Join Date
    Nov 2001
    Posts
    10,550
    Thanks
    0
    Thanked 7 Times in 7 Posts
    [quote name='chrisgreaves' post='793166' date='13-Sep-2009 13:22']I hesitate to contact a Microsoft MVP about this, but anyone else here may care to volunteer.
    I feel that the technique of using
    Code:
    	'Undo the replace
    	 ActiveDocument.Undo
    is a Trap For Young Players, since the .Undo also undoes any changes made immediately prior to the procedure call.[/quote]
    I have used this code with no problems. ActiveDocument.Undo only undoes the most recent action (unless you give it an integer parameter saying how many changes you want to Undo).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •