How can I automatically close a word dialog box?

Posted on 2008-02-03
Medium Priority
Last Modified: 2012-05-05
Hi Experts,

I'm testing a Word add-in with a script that runs it for lots and lots of documents.  It does it like this:

Dim fso As Object, fld As Object, fil As Object
Dim WasOpen As Boolean
Dim d As Document
Set fso = CreateObject("Scripting.FileSystemObject")

Set fld = fso.GetFolder("E:\My Documents\")

For Each fil In fld.Files
        If UCase(Right(fil.Name, 4)) = ".DOC" Then
            On Error Resume Next
            Set d = Documents(fil.Name)
            If Err <> 0 Then
                WasOpen = False
                Set d = Documents.Open(fil.Path)
                WasOpen = True
            End If
            Set doctesting = Application.ActiveDocument
            If doctesting.ProtectionType = wdNoProtection Then
                Load UserForm
                Call UserForm.StartButton_Click
However, sometimes the script is interrupted by a Word dialog that comes up before the script reaches reaches call. 'startbutton_click.  Specifically, two dialogs sometimes get in the way:
- If the document is password protected then there is a dialog to enter the password or click 'read only'
- A dialog box that says "Problem During Load" - Missing file: etc.  And you need to click 'ok'

Neither comes up very often - but when they do, the whole script is interrupted... so I can lose hours of testing time before I find out.  Is there any way to automatically answer 'read only' if the password protection one comes up or 'ok' if the problem during load comes up?

Thanks in advance for any advice.


Question by:PatternNut
LVL 19

Expert Comment

by:William Elliott
ID: 20812266
on error resume next ignore these, but for the specific results you want i utilize a program called 'autoit' you can create and adlib function that hangs out until it sees a specific windows, then runs a function like clicking read only or ok. you should be able to run the autoit from your vb code. or reference the dll to add it directly to vb6

LVL 77

Accepted Solution

GrahamSkan earned 2000 total points
ID: 20813053
You can supress the showing of information-only dialogues with

wdApp.DisplayAlerts = False

where wdApp is the Word application.

Otherwise you will have to anticipate and cater for the dialogues.

If you are opening a document that is possible protected, you can provide a random password. If it is right, or none is needed, the document will open.  If it is wrong, you will get a trappable error.

The snippet is a Word macro.

You can check for a file's existence so:

If Dir$("C:\MyFolder\MyFile.Doc") = "" then
     'File is missing
     set wdDoc = wdApp.Documents.Open("C:\MyFolder\MyFile.Doc")

Sub OpenDoc(strFileSpec As String)
    Dim doc As Document
    On Error GoTo OpenDocError
    Application.DisplayAlerts = False
    Set doc = Documents.Open(strFileSpec, , , , "anytext1", , , "anytext2")
    Exit Sub
    Select Case Err.Number
        Case 5408 'The password is incorrect. Word cannot open the document.
            Resume OpenDocExit
        Case Else
            MsgBox "Unexpected Error; " & Error.Description
    End Select
End Sub

Open in new window


Author Comment

ID: 20828830
Nice one Graham, this worked the charm.

Thank you,


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This is written from a 'VBA for MS Word' perspective, but I am sure it applies to most other MS Office components where VBA is used.  One thing that really bugs me is slow code, ESPECIALLY when it's mine!  In programming there are so many ways to…
Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
This video walks the viewer through the process of creating an MLA formatted document, as well as a bibliography with citations.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question