Making Modal Dialog Boxes Appear in Front of Workbooks

Written by Allen Wyatt (last updated May 23, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


5

Don tends to work on two monitors and have multiple workbooks open on the same instance of Excel. He also maximizes his windows. (He describes himself as a "see everything in one shot" type of guy.) Recently, when Don has done something that opens a modal dialog box, the box opens behind the workbook, which stops him from seeing the dialog box or interacting with it. He wonders if there is a way he can get the modal dialog boxes to appear in front of the workbook.

Before answering, it should be pointed out that dialog boxes can be of two types: modal and modeless. A modeless dialog box is one that is virtually independent of the program to which it belongs. A good example of such a dialog box is the Find and Replace dialog box. On the other hand, a modal dialog box (the ones that Don is having problems with) must be responded to or closed before you can continue working with the program to which the dialog box belongs.

It is unclear whether Don's problem is with modal dialog boxes generated by Excel itself or if they are dialog boxes generated by macros over which Don has control. If it is the former, then there is very little that can be done; the layered location of the dialog box is controlled entirely by the program (and the programmers that created it). In other words, it would take a code change to make the dialog box appeared layered on top of the workbook instead of under it.

Understand that there is a scenario in which the problem may not be due to an Excel programming problem. It could be that you have a third-party application running on the system which affects the layering of dialog boxes and their parent windows. The typical culprits in this scenario are memory-resident utilities that force themselves to always appear on top of whatever else is on the screen. The only way to see if this is the culprit is to disable the loading of such utilities and, within Excel, display a modal dialog box. If the behavior returns to normal, you then know the source of the problem.

If it is the case, however, that the dialog box is generated by a macro Don developed then the solution is to adjust the code that generates the modal dialog box. (In Excel macros these types of dialog boxes are typically implemented through userforms.).

The problem with userform placement in multiple screen scenarios is overcome by the manual placement of the userform within its initiating code. For instance, you can use some variation on this:

Load UserForm1
UserForm1.StartUpPosition = 0
UserForm1.Top = Application.Top + 25
UserForm1.Left = Application.Left + 25
UserForm1.Show

You may need to experiment with the placement but setting the StartUpPosition property to 0 is required so that VBA knows you want to manually location the userform.

Note:

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

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13349) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365.

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

Finding the Smallest Even Value

When processing data in a worksheet, you may have a need to know what the smallest (lowest) even value in a range is. You ...

Discover More

Toggling AutoCorrect Settings

If you need to turn AutoCorrect on or off, there is no built-in way to easily do it in Word. You can create your own ...

Discover More

Sign-in Sheets

Printed sign-in sheets are a staple at many meetings and seminars. Word can create them lickety-split just by using a few ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Macro, while Running, Stops Excel from Responding

When running a macro, have you ever seen Excel appear to stop responding? This can be frustrating, but there are a couple ...

Discover More

Adding Leading Zeroes to ZIP Codes

Import a bunch of ZIP Codes into Excel, and you may be surprised that any leading zeroes disappear. Here's a handy little ...

Discover More

Determining the Length of a String

Macros are great for working with strings, and one of the most commonly used string functions is Len. This tip explains ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, 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 8 + 7?

2020-09-23 02:57:02

Mike Fairley

Thanks J Woollley. They are all great suggestions, I will try them out and let you know how it works. I am learning a lot!

BTW - We have been struggling with this for more than a year for a large client. The dialog behaviour is very unpredictable. On some devices/version of Excel it is fine, and others not. I have a computer where it *always* goes to the back.


2020-09-22 15:59:28

J. Woolley

@Mike Fairley
I believe my previous suggestion will not work for this case. When I test your code with my configuration (after adjusting your oApp/olApp variables), the address book always appears in the foreground. I even tested calling your code from a UserForm, which you implied in your original question. Since the address book does not appear in the foreground for your configuration, try adding the following lines before and after the With... section:

Application.WindowState = xlMinimized
With oDialog
...
End With
Application.WindowState = xlNormal

If your code is called from a UserForm, try changing the form's ShowModal property to False. If that fails, try calling your code from a standard module and passing the results to your form's code.
If you cannot resolve the issue, this would be a good question for wellsr VBA Q&A: https://ask.wellsr.com/vba/
You might also be interested in My Excel Toolbox: https://sites.google.com/view/MyExcelToolbox/


2020-09-22 06:28:29

Mike Fairley

Thanks J Wooley, appreciate your suggestion. I am not sure where to put your code.

Sub AddressBookName()

Dim oApp As Outlook.Application
Dim oDialog As SelectNamesDialog
Dim oGAL As AddressList
Dim myAddrEntry As AddressEntry
Dim exchUser As Outlook.ExchangeUser

Dim AliasName As String
Dim FirstName As String
Dim LastName As String
Dim EmailAddress As String

Set oApp = CreateObject("Outlook.Application")
Application.Wait (Now + TimeValue("0:00:05"))
Set oDialog = olApp.Session.GetSelectNamesDialog
Set oGAL = olApp.GetNamespace("MAPI").AddressLists("Global Address List")

With oDialog
.AllowMultipleSelection = False
.InitialAddressList = oGAL
.ShowOnlyInitialAddressList = True
If .Display Then
--------------'this is the point where the Outlook dialog is displayed ---------------
AliasName = oDialog.Recipients.Item(1).Name
Set myAddrEntry = oGAL.AddressEntries(AliasName)
Set exchUser = myAddrEntry.GetExchangeUser

If Not exchUser Is Nothing Then
FirstName = exchUser.FirstName
LastName = exchUser.LastName
EmailAddress = exchUser.PrimarySmtpAddress
End If
End If
End With

Set olApp = Nothing
Set oDialog = Nothing
Set oGAL = Nothing
Set myAddrEntry = Nothing
Set exchUser = Nothing
End Sub


2020-09-21 10:41:15

J. Woolley

@Mike Fairley
You can try adding this line to your VBA after opening OAB:
CreateObject("WScript.Shell").AppActivate sOAB
where sOAB is a String identifying all or part of the OAB's titlebar caption. For example:
sOAB = "Address Book"


2020-09-20 22:13:06

Mike Fairley

Hi Allen. Love your work. Your site is my "goto" for Excel queries.

I have a related issue. When I call the Outlook Address Book from an Excel VBA form. The OAB always goes behind the Excel application. Any ideas how to resolve??


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, 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.