Please Note: This article is written for users of the following Microsoft Word versions: 97, 2000, 2002, and 2003. If you are using a later version (Word 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Word, click here: Detecting an Open Dialog Box.

Detecting an Open Dialog Box

Written by Allen Wyatt (last updated April 1, 2023)
This tip applies to Word 97, 2000, 2002, and 2003


Thomas has a macro that performs various functions at timed intervals. Some of those functions should not be performed if a dialog box is open on the screen, because trying to perform them will cause an error in the functions. He wonders if there is some way, within the macro, to detect if a dialog box—any dialog box—is currently open. Thomas would rather detect the open dialog box rather than deal with a generated error.

Perhaps the only way to attempt to do this is to use FindWindow, which is actually part of the Windows API. Its purpose is to retrieve a handle to a particular open window. (A dialog box is nothing but an open window, and each dialog box has a name.) This approach won't tell you if any dialog box is open, but it will tell you if a specific dialog box is open.

Here is a quick example to show how the FindWindow function can be used:

Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
  (ByVal wClassName As Any, ByVal wWindowName As String) As Long

Sub testDialogOpen()
    Dim wHandle As Long
    Dim wName As String

    wName = "Find and Replace"
    wHandle = FindWindow(0&, wName)
    If wHandle = 0 Then
        MsgBox "Dialog window is not open"
    Else
        MsgBox "Dialog window is open"
    End If
End Sub

Note that the FindWindow function needs to be declared outside of your VBA procedure. Then, within the procedure, you need to specify the name of the dialog box you want to find out about. This is the name that appears in the title bar for the dialog box, and the FindWindow function is case insensitive. The return value for FindWindow will be a handle for the dialog box, if it is open. If it is not open, then the function returns a 0.

Remember, too, that dialog boxes can be of two types: modal and non-modal. If a particular dialog box is modal, then it must be dismissed before any other actions can be taken on the system. You'll want to do extensive testing with the dialog boxes at point, as you may get different performance from your macro depending on whether an open dialog box is modal or not.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the WordTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (12355) applies to Microsoft Word 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Word (Word 2007 and later) here: Detecting an Open Dialog Box.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Adding Quotes

Adding quote marks is normally as simple as typing them from the keyboard. However, if you want to add quote marks around ...

Discover More

Moving Object Anchors

When you insert an object into your document, it is anchored to a paragraph. If you want to change the paragraph to which ...

Discover More

Increasing the AutoFilter Drop-Down Limit

When you turn on AutoFiltering, Excel displays a drop-down list at the top of each column in your data table. This list ...

Discover More

Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!

More WordTips (menu)

Printing via Macro without Messages

When you are printing a document, it is not unusual to see messages (dialog boxes) periodically. When you want the ...

Discover More

Occurrences of a Text String within a Document

You may have a need to find out how many times a certain text string occurs within a document. You can find out manually ...

Discover More

Highlight Words from a Word List

Do you need to highlight certain words in a document, and aren't quite sure how to go about it? Using the techniques ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is one less than 9?

There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)


This Site

Got a version of Word that uses the menu interface (Word 97, Word 2000, Word 2002, or Word 2003)? This site is for you! If you use a later version of Word, visit our WordTips site focusing on the ribbon interface.

Videos
Subscribe

FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.