Speed up a macro

<Split from unrelated thread>

I know it sounds incredibly pathetic but this forum does not make it obvious how to start new threads and I cannot figure out how to do it.  In any case, I have the following code.  It takes about 3 or 4 seconds to execute.  I was wondering if anyone knew how to speed it up.

[code]

                                                

With ActiveDocument.Content.Find
    .Text = "a^"
    .Replacement.Text = ChrW(&H2200)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

With ActiveDocument.Content.Find
    .Text = "pr+"
    .Replacement.Text = ChrW(&H3C6)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "x^"
    .Replacement.Text = ChrW(&H2261)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

 With ActiveDocument.Content.Find
    .Text = "c^"
    .Replacement.Text = ChrW(&H21D2)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "[["
    .Replacement.Text = ChrW(&H27E6)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "]]"
    .Replacement.Text = ChrW(&H22E7)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "ta+"
    .Replacement.Text = ChrW(&H22A4)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "co+"
    .Replacement.Text = ChrW(&H22A5)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "no+"
    .Replacement.Text = ChrW(&H418)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "ab+"
    .Replacement.Text = ChrW(&H2207)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "ng+"
    .Replacement.Text = ChrW(&H2310)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "nq+"
    .Replacement.Text = ChrW(&H2985)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "//"
    .Replacement.Text = ChrW(&H2997)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "\\"
    .Replacement.Text = ChrW(&H2998)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "ac+"
    .Replacement.Text = ChrW(&H4D2)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "m^"
    .Replacement.Text = ChrW(&H3A0)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "n^"
    .Replacement.Text = ChrW(&H25A1)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "p^"
    .Replacement.Text = ChrW(&H22C4)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "t^"
    .Replacement.Text = ChrW(&H2192)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "xor"
    .Replacement.Text = ChrW(&H2295)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "i^"
    .Replacement.Text = ChrW(&H22A2)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "en+"
    .Replacement.Text = ChrW(&H22A8)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "f^"
    .Replacement.Text = ChrW(&H3C0)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "e^"
    .Replacement.Text = ChrW(&H2203)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

  With ActiveDocument.Content.Find
    .Text = "va+"
    .Replacement.Text = ChrW(&H3B1)
    .Font.Size = 12
    .Replacement.ClearFormatting
    .MatchWildcards = False
    .Execute Replace:=wdReplaceAll
  End With

[/code]

Answer
Answer

If you're doing this many replacements on a large document, it is going to take some time -- 3 or 4 seconds doesn't seem excessive.

Setting the .MatchWildcards = False in each find is probably not necessary, because that's the default value, but I doubt that removing those lines will noticeably speed up the macro execution.

I see that you had a thread in 2010 about making multiple replacements. That sort of macro will work for this; the VBA for find/replace in Word 2013 is identical to what you used in Word 2003.

As for starting a new thread, I replied to another of your posts (which I moved to the Community Participation forum) with directions.

_____________________________
https://jay-freedman.info

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

Answer
Answer

Something that would likely expedite processing is to use:

Application.ScreenUpdating = False

in each macro (better to combine the macros first, though), as in:

Application.ScreenUpdating = False

With ActiveDocument.Content.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .Format = True
  .Font.Size = 12
  .Forward = True
  .Wrap = wdFindContinue
  .MatchWildcards = False
  .Text = "a^"
  .Replacement.Text = ChrW(&H2200)
  .Execute Replace:=wdReplaceAll
  .Text = "pr+"
  .Replacement.Text = ChrW(&H3C6)
  .Execute Replace:=wdReplaceAll
  .Text = "x^"
  .Replacement.Text = ChrW(&H2261)
  .Execute Replace:=wdReplaceAll
  .Text = "c^"
  .Replacement.Text = ChrW(&H21D2)
  .Execute Replace:=wdReplaceAll
  .Text = "[["
  .Replacement.Text = ChrW(&H27E6)
  .Execute Replace:=wdReplaceAll
  .Text = "]]"
  .Replacement.Text = ChrW(&H22E7)
  .Execute Replace:=wdReplaceAll
  .Text = "ta+"
  .Replacement.Text = ChrW(&H22A4)
  .Execute Replace:=wdReplaceAll
  .Text = "co+"
  .Replacement.Text = ChrW(&H22A5)
  .Execute Replace:=wdReplaceAll
  .Text = "no+"
  .Replacement.Text = ChrW(&H418)
  .Execute Replace:=wdReplaceAll
  .Text = "ab+"
  .Replacement.Text = ChrW(&H2207)
  .Execute Replace:=wdReplaceAll
  .Text = "ng+"
  .Replacement.Text = ChrW(&H2310)
  .Execute Replace:=wdReplaceAll
  .Text = "nq+"
  .Replacement.Text = ChrW(&H2985)
  .Execute Replace:=wdReplaceAll
  .Text = "//"
  .Replacement.Text = ChrW(&H2997)
  .Execute Replace:=wdReplaceAll
  .Text = "\\"
  .Replacement.Text = ChrW(&H2998)
  .Execute Replace:=wdReplaceAll
  .Text = "ac+"
  .Replacement.Text = ChrW(&H4D2)
  .Execute Replace:=wdReplaceAll
  .Text = "m^"
  .Replacement.Text = ChrW(&H3A0)
  .Execute Replace:=wdReplaceAll
  .Text = "n^"
  .Replacement.Text = ChrW(&H25A1)
  .Execute Replace:=wdReplaceAll
  .Text = "p^"
  .Replacement.Text = ChrW(&H22C4)
  .Execute Replace:=wdReplaceAll
  .Text = "t^"
  .Replacement.Text = ChrW(&H2192)
  .Execute Replace:=wdReplaceAll
  .Text = "xor"
  .Replacement.Text = ChrW(&H2295)
  .Execute Replace:=wdReplaceAll
  .Text = "i^"
  .Replacement.Text = ChrW(&H22A2)
  .Execute Replace:=wdReplaceAll
  .Text = "en+"
  .Replacement.Text = ChrW(&H22A8)
  .Execute Replace:=wdReplaceAll
  .Text = "f^"
  .Replacement.Text = ChrW(&H3C0)
  .Execute Replace:=wdReplaceAll
  .Text = "e^"
  .Replacement.Text = ChrW(&H2203)
  .Execute Replace:=wdReplaceAll
  .Text = "va+"
  .Replacement.Text = ChrW(&H3B1)
  .Execute Replace:=wdReplaceAll
End With

Application.ScreenUpdating = True

This would execute about as fast as using a loop, though I'd still prefer to use one. Other optimizations are possible if the content to be processed is in tables.

Ultimately, though, (as Jay says) the time taken is largely dependent on the size of the document and how many expressions there are to process.

Another option is to use an Excel workbook to hold the Find/Replace strings. For an example of that, see: http://www.msofficeforums.com/word-vba/12803-find-replace.html#post34254

Note: The macro there is designed to process all documents in a folder. To work with just one document, you could either modify the code or put just that document into the folder to be processed.

Cheers
Paul Edstein
(Fmr MS MVP - Word)

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 754 Applies to: