How can I automatically close a word dialog box?

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.


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

William ElliottSr Tech GuruCommented:
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
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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PatternNutAuthor Commented:
Nice one Graham, this worked the charm.

Thank you,

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.