How to speed up Word Automation by hiding the application

And a few gotchas

Article contributed by Daryl Lucas

Many people know they can speed execution of Word Automation by turning off screen updating:

Word.Application.ScreenUpdating = False

Many do not know, however, that they can get an even greater speed boost by hiding the application altogether. Here is an example from a Visual Basic client:

Private Sub SayHello()

On Error GoTo CATCH
Dim oWordApp As Word.Application

Set oWordApp = New Word.Application
With oWordApp
    Dim oWordDoc As Word.Document
    Set oWordDoc = .Documents.Add
End With

With oWordDoc
    .Content.Text = "Hello, World!"
    .SaveAs "Hello"
    .Close
End With

Set oWordDoc = Nothing
oWordApp.Quit
Set oWordApp = Nothing

EXITHERE
    Exit Sub

CATCH:
    Err.Raise Err.Number, Err.Source, Err.Description
    Resume EXITHERE

End Sub

In the above example, Word launches but does not appear anywhere on the screen. It does not even show up in the Taskbar. (It does, though, show up in NT's Task Manager, in its list of running processes.) Despite this apparent lack of response, Word is very active and quite capable of doing everything it is told-creating a new document, inserting the message, Hello, World!, saving the file, closing it, and quitting. You can verify this by launching Word the old-fashioned way and opening File1 at the bottom of the File menu after running the sample code.

Although it would be difficult to do an exhaustive test, in theory this should work from any Automation client-Visual Basic, Excel, PowerPoint, or any other. The functionality depends on Word, not on the Automation client.

But it also works even if you run your Automation code from Word itself. Word does not need to display itself in order to run. In the above code, you could omit the lines that create and use the Word.Application object and replace them with invisibility lines:

Private Sub InvokeStealthMode()

On Error GoTo CATCH

With Word.Application
    .Visible = False
    Dim oWordDoc As Word.Document
    Set oWordDoc = .Documents.Add
End With

With oWordDoc
    .Content.Text = "Hello, World!"
    .SaveAs "Hello"
    .Close
End With

Set oWordDoc = Nothing

EXITHERE:
    On Error Resume Next
    Word.Application.Visible = True
    Exit Sub

CATCH:
    Err.Raise Err.Number, Err.Source, Err.Description
    Resume EXITHERE

End Sub

Notice that you want to put the Word.Application.Visible = True line in an error-handler or in a spot where you know it will be run if something goes awry. Do not assume that everything will always go fine. (If you do get stuck with an invisible Word in the middle of a crash, you can launch the Task Manager and kill the WINWORD.EXE process.)
How much of a difference does invisibility make? Your mileage may vary, but in the informal testing I've done, I've found that Visible = False makes Word work roughly 15% faster than ScreenUpdating = False does on identical tasks. That's about 1 second for every 7-not a trivial amount if your job runs longer than that.

Caveats? Chief among them is that repagination routines don't work when Word is invisible. If you need to update page numbers, you will have to show the application window before doing the update:

With Word.Application
    .Visible = True
    oWordDoc.Repaginate
    .Visible = False
End With

See Page X of Y displays or prints as Page 1 of 1, Page 2 of 2 etc.  for details on dealing with repagination problems.

I hear rumors that you may also need to work with the Range object instead of the Selection object, but I have not tested this nearly enough to give a list because I do almost all of my work with Ranges.