Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. If you are using an earlier version (Word 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Word, click here: Detecting an Open Dialog Box.

Detecting an Open Dialog Box

Written by Allen Wyatt (last updated May 11, 2022)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365


4

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 some 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 (12356) applies to Microsoft Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365. You can find a version of this tip for the older menu interface of Word 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 and Using a Combo Box

Combo boxes can be a great way of getting user input in a worksheet. Here's how to add a combo box to your worksheet and ...

Discover More

Permanent Watermarks in a Document

Need to add a graphic watermark to a document? It's not that hard to do, but making the watermark permanent can be a bit ...

Discover More

Changing the Size of a Drawing Object

Documents are often made up of more than just text. If you have drawing objects in your document, you will doubtless need ...

Discover More

Learning Made Easy! Quickly teach yourself how to format, publish, and share your content using Word 2013. With Step by Step, you set the pace, building and practicing the skills you need, just when you need them! Check out Microsoft Word 2013 Step by Step today!

More WordTips (ribbon)

Displaying Properties Dialog Box in a Macro

Word keeps track of identifying and statistical information about a document and makes that information available in the ...

Discover More

Moving the Insertion Point to the Beginning of a Line

If you need to move the insertion point within your macro, then you'll want to note the HomeKey method, described in this ...

Discover More

Printing Summary Information from a Macro

Part of the information that Word maintains about each of your documents is a summary statement, which you can define in ...

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 two more than 7?

2024-09-23 17:55:54

Peter A. Tanner

Hi Allen,

This statement at the top of the article is not correct: "This tip applies to Word 2007, 2010, 2013, 2016, 2019, and Word in Microsoft 365"

I would like to correct my post made back in May...

I have at last found Microsoft's documentation on the FindWindow declaration for all Word version using VBA7 on 64-bit Windows. They give the following, and it works:

Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr

Best regards,
Peter


2024-05-11 10:09:35

Peter A. Tanner

G'day Allen,

Thanks for this tip! Just what I needed!

For 64bit Word, the declaration has to be done like this:
Declare PtrSafe Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal wClassName As Any, ByVal wWindowName As String) As LongPtr

Best regards,
Peter


2022-05-11 17:32:16

Chester Hood

Since FindWindow is part of the Windows API, will this approach work with any of the Office VBA products? Specifically, I have VBA code in Excel workbooks, Access applications, and Outlook inbox processing. Thanks.


2022-05-11 10:21:54

Philip

Is there a way to do this on a Mac?


This Site

Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu 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.