Please Note: This article is written for users of the following Microsoft Word versions: 2007, 2010, 2013, 2016, 2019, 2021, 2024, 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.
Written by Allen Wyatt (last updated December 6, 2025)
This tip applies to Word 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Word in Microsoft 365
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:
#If VBA7 Then
Declare PtrSafe Function FindWindow Lib "user32" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As LongPtr
#Else
Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
#End If
Sub testDialogOpen()
#If VBA7 Then
Dim wHandle As LongPtr
#Else
Dim wHandle As Long
#End If
Dim wName As String
wName = "Find and Replace"
wHandle = FindWindow(vbNullString, wName)
If wHandle = 0 Then
MsgBox "Dialog window is not open"
Else
MsgBox "Dialog window is open"
End If
End Sub
Note that the conditional compilation block at the beginning of the code, used to define the proper FindWindow function, needs to be declared outside of your VBA procedure. This conditional block is used to properly handle if you are using VBA7 or not, as this affects the proper syntax to be used in the code. Word 2007 used VBA6, with VBA7 being introduced beginning with Word 2010.
Within the testDialogOpen procedure, you need to specify, in the wName variable, 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:
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, 2021, 2024, 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.
The First and Last Word on Word! Bestselling For Dummies author Dan Gookin puts his usual fun and friendly candor back to work to show you how to navigate Word 2019. Spend more time working and less time trying to figure it all out! Check out Word 2019 For Dummies today!
Program a macro, and you can easily find that some lines get very long. If you want to shorten the lines so they are more ...
Discover MoreSaving a document in a different format is easy if you are manually using the Save As command. Saving a document in an ...
Discover MoreWhen processing a document using a macro, you may need to know the precise size of a particular file. The way you figure ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2025 Sharon Parq Associates, Inc.
Comments