none
Microsoft Word 2010 Macro very slow, How can I speed it up? RRS feed

  • Question

  • Hi everybody

    I have created a Microsoft word macro for searching specific lines in the document and either delete them or change the document style, applying a Doc Heading style.

    The macro is working fine in small documents (up to 15 pages) but when I run it on bigger documents (up to 130 pages) it takes forever to complete!

    Is there some tip that I can use in order to speed it up? 

    This is the first time I use word VB macros, so I am quite a beginner. Here is my macro code:

    Sub Test2()

        Dim par As Paragraph
        Dim rng As Range
        Dim set_doc2 As Integer
        Dim set_doc3 As Integer
        
        
        set_doc2 = 0   
        set_doc3 = 0
        
        For Each par In ThisDocument.Paragraphs
            Set rng = par.Range
            If (set_doc2 = 1) Then
                rng.Style = ActiveDocument.Styles("Doc Heading 2")
                set_doc2 = 2
            ElseIf (set_doc3 = 1) Then
                rng.Style = ActiveDocument.Styles("Doc Heading 3")
                set_doc3 = 2
            Else
                If Left(rng.Text, 59) = "-----------------------------------------------------------" Then
                    rng.Delete
                    If (set_doc2 = 2) Then
                        set_doc2 = 0
                    Else
                        set_doc2 = 1
                    End If
                ElseIf Left(rng.Text, 43) = "-------------------------------------------" Then
                    rng.Delete
                    If (set_doc3 = 2) Then
                        set_doc3 = 0
                    Else
                        set_doc3 = 1
                    End If
                End If
            End If
        Next
        Set rng = Nothing
        Set par = Nothing
    End Sub

    Thanks in advance for any reply!

    Tt

    Wednesday, February 27, 2013 1:37 PM

Answers

  • Thanks for your reply.

    The document is very simple, just text.

    However I changed a bit the structure of the text so that more simple keyword must be found in the text and it seems it's now executing quite fast. That without modifying anything in my code, which is quite weird.

    Thanks abyway for the suggestions, I'll keep them in mind for the future!

    Thursday, February 28, 2013 1:35 PM
  • Without knowing more about the structure of the document you're trying to process, it's not possible to advise on other approaches that might be more efficient. The revisions to your code below should be faster, mainly through turning off ScreenUpdating and eliminating redundancy:

    Sub Test2()
    Application.ScreenUpdating = False
    Dim par As Paragraph, set_doc2 As Integer, set_doc3 As Integer
    set_doc2 = 0: set_doc3 = 0
    With ActiveDocument
      For Each par In .Paragraphs
        If set_doc2 = 1 Then
          par.Style = "Doc Heading 2"
          set_doc2 = 2
        ElseIf set_doc3 = 1 Then
          par.Style = "Doc Heading 3"
          set_doc3 = 2
        Else
          With par.Range
            If Left(.Text, 59) = "-----------------------------------------------------------" Then
              .Delete
              set_doc2 = (set_doc2 + 1) Mod 2
            ElseIf Left(.Text, 43) = "-------------------------------------------" Then
              .Delete
              set_doc3 = (set_doc3 + 1) Mod 2
            End If
          End With
        End If
      Next
    End With
    Set par = Nothing
    Application.ScreenUpdating = True
    End Sub

    Cheers
    Paul Edstein
    [MS MVP - Word]


    Wednesday, February 27, 2013 10:12 PM

All replies

  • Without knowing more about the structure of the document you're trying to process, it's not possible to advise on other approaches that might be more efficient. The revisions to your code below should be faster, mainly through turning off ScreenUpdating and eliminating redundancy:

    Sub Test2()
    Application.ScreenUpdating = False
    Dim par As Paragraph, set_doc2 As Integer, set_doc3 As Integer
    set_doc2 = 0: set_doc3 = 0
    With ActiveDocument
      For Each par In .Paragraphs
        If set_doc2 = 1 Then
          par.Style = "Doc Heading 2"
          set_doc2 = 2
        ElseIf set_doc3 = 1 Then
          par.Style = "Doc Heading 3"
          set_doc3 = 2
        Else
          With par.Range
            If Left(.Text, 59) = "-----------------------------------------------------------" Then
              .Delete
              set_doc2 = (set_doc2 + 1) Mod 2
            ElseIf Left(.Text, 43) = "-------------------------------------------" Then
              .Delete
              set_doc3 = (set_doc3 + 1) Mod 2
            End If
          End With
        End If
      Next
    End With
    Set par = Nothing
    Application.ScreenUpdating = True
    End Sub

    Cheers
    Paul Edstein
    [MS MVP - Word]


    Wednesday, February 27, 2013 10:12 PM
  • Thanks for your reply.

    The document is very simple, just text.

    However I changed a bit the structure of the text so that more simple keyword must be found in the text and it seems it's now executing quite fast. That without modifying anything in my code, which is quite weird.

    Thanks abyway for the suggestions, I'll keep them in mind for the future!

    Thursday, February 28, 2013 1:35 PM