Increase Speed of a Word Macro

Idea
I have written a macro in MS-Word for formatting of documents. It finds the carriage Return character and replaces it with Null. For a 60 page document the macro takes approx 1 hour to complete. I have tried following options

1. Increase Virtual Memory
2. Increase RAM from 512 Mb to 1024 Mb.
3. Turned Off Spell Check and Grammar Check.

but nothing has helped. Can anyone please tell me how to increase the speed and decrease the execution time of the macro.



Answer this question

Increase Speed of a Word Macro

  • Meareg

    Im not sure if you have used word count for any specific reason

    If you are using the macro with the activedocument then

    Sub ReplaceCarriageReturnsWithNull()

    Selection.HomeKey wdStory, wdMove
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "^p"
        .Replacement.Text = ""
        .Execute Replace:=wdReplaceAll
    End With

    End Sub

    would do the replacement of the carriage return with nothing
    You can also use the chr function chr(13) or chr(10) to find the carriage returns

    The code is executing the Replace command throgh a macro.

    Use
    selection.find.wildcards= false
    for avoiding errors due to wildcards



  • Dario de Judicibus

    Sub Format_Doc()
    Dim i As Long
    Dim WordCount As Long
    On Error GoTo err
        MsgBox Time
        '** Get the total word count of the document in a variable **
        WordCount = ActiveDocument.Words.Count
       
        For i = 1 To (WordCount - 3)
            If i > WordCount Or i >= ActiveDocument.Words.Count Then
                GoTo over
            End If

            '** Check if there is an <ENTER> key anywhere in a paragraph **
            If ActiveDocument.Words(i) = vbCr Then
                If ActiveDocument.Words(i - 1).Bold = True Or ActiveDocument.Words(i - 1).Case = vbUpperCase Or Asc(ActiveDocument.Words(i - 1)) = Asc(".") Or Asc(ActiveDocument.Words(i - 1)) = Asc(",") Or Asc(ActiveDocument.Words(i - 1)) = Asc(":") Or Asc(ActiveDocument.Words(i - 1)) = Asc(";") Or ActiveDocument.Words(i - 1) = vbCrLf Or ActiveDocument.Words(i - 1) = vbCr Or ActiveDocument.Words(i - 2) = vbCr Or ActiveDocument.Words(i - 3) = vbCr Or Asc(ActiveDocument.Words(i - 1)) = Asc(""".") Then
                    '** Do nothing here **
                Else
                    '** If all words in a document are checked then end the process **
                    If i > WordCount Then GoTo over
                    '** Check for these specific characters around the <ENTER> key **
                    If Asc(ActiveDocument.Words(i - 1)) <> 32 And Asc(ActiveDocument.Words(i - 2)) <> 32 And Asc(ActiveDocument.Words(i - 1)) <> Asc(").") And Asc(ActiveDocument.Words(i - 1)) <> Asc("/-.") And Asc(ActiveDocument.Words(i + 1)) <> Asc("*") And Asc(ActiveDocument.Words(i + 2)) <> Asc("*") Then
                        '** Remove the <ENTER> key **
                        ActiveDocument.Words(i) = Replace(ActiveDocument.Words(i), vbCr, "", , , vbBinaryCompare)
                        '** After removing an <ENTER> key decrease the total count of the words in a document **
                        WordCount = ActiveDocument.Words.Count
                    End If
                End If
            End If
        Next

    over:
        '** Display a message box as soon as the formatting of the document is completed **
        MsgBox "Document formatting Completed!" & vbCrLf & "Please SAVE the document now.", vbInformation, "Format Complete"
    Exit Sub

    err:
        MsgBox err.Number & "   " & err.Description & "   - " & ActiveDocument.Words(i - 1) & " " & ActiveDocument.Words(i - 2) & " " & ActiveDocument.Words(i - 3)
        Resume Next
    End Sub


  • Jesper Arent DK

    Neelendra,

    Can show us you Script, cos I can't see how or why it should take that long.

    Gnomie


  • KPLNG

    The key to this is that it is not just relacing "^p", because if the "^p" is proceded by "." then it leave it alone...

    I think the answer might be to:

    • REPLACE ALL "^p" with "This_is_Paragraph_mark"
    • REPLACE ALL ".This_is_Paragraph_mark" with "^p"
    • REPLACE ALL ":This_is_Paragraph_mark" with "^p"
    • REPLACE ALL ";This_is_Paragraph_mark" with "^p"
    • etc

    Then

    • REPLACE ALL "This_is_Paragraph_mark" with ""

  • Increase Speed of a Word Macro