Make a Form of Your Own Modal Against Other Processes

This macro prevents users from working in another program while your program is waiting for an input.

Last modified: 2006/02/05 | Accessed: 88.168  | #22
◀ Previous sample Next sample ▶
Category-Manager Category-Manager
With Category-Manager you can group your Outlook categories, share them with other users, filter a folder by category, automatically categorize new emails, and more. You can use the Addin even for IMAP.

Frequently, Outlook developers need to display a formular to a user to request some data while the user is working on an e-mail.

There's a problem if Word is used as e-mail editor: Your Addin or VBA code runs in the process of Outlook while the e-mail editor runs in the process of Word. That means for instance, you cannot show your formular modal to the mail window; without tricks you even cannot get the mail window on top or give it the focus.

Dr. Jürgen Thümmler provides the DLL dsmodal for free: With that you can easily make any window modal to a window of an other process or even make it modal to all the system.

This sample demonstrates how to obtain the window handles and use dsmodal. Please copy the dsmodal.dll into the system directory (..winntsystem32) so that it will be found.

The sample uses a VB6 formular. Before loading it, please set the ParentWindowCaption to the caption of the mail window (Inspector.Caption).

With a few changes you can also use it in VBA code. Because the UserForm of the Office library doesn't know the Form_Load event, you could call the code e.g. once in the Inspector_Activate event. Additionally, the UserForm doesn't have a property for its window handle. But with its caption property and the FindChildWindowText function you can get the handle easily.

tip  How to add macros to Outlook
Private Declare Function MakeModal& Lib "dsmodal" _
  (ByVal AppHwnd&, ByVal hwndDest&, Optional ByVal Beep& = 0)

Public ParentWindowCaption As String

Private Sub Form_Load()
  Dim lParent As Long
  Dim lMe As Long

  lParent = FindChildWindowText(GetDesktopWindow, ParentWindowCaption)

  ' VB Forms:
  lMe = Me.hwnd

  Call MakeModal(lMe, lParent, 1)
End Sub

Private Sub Form_Unload(Cancel As Integer)
  Call MakeModal(0, 0, 0)
End Sub
Reporter Reporter
VBOffice Reporter is an easy to use tool for data analysis and reporting in Outlook. A single click, for instance, allows you to see the number of hours planned for meetings the next month.
email  Send a message