modal dialog, center outlook window, disable outlook window

J

J.P.

I've developed a custom form that it is triggered from a button click
where the button is tied to a custom macro. The form is being loaded
from the macro code. With that said, I'm getting unexpected/
unacceptable behavior from the form.

1. Even though the dialog is a modal dialog, the outlook window is
still active when the dialog is being shown. The window still remains
on top of the outlook window even if focus is shifted to the main
outlook window. This is a problem because the actions the macro
performs are dependent on the items selected in outlook at the time
the button was clicked so I do not want the selections to be changed
'by accident' due to the user clicking around.

2. I have the form set to start at the center of the parent but it
does not. I am working on a dual monitor system and the dialog always
opens on the second monitor even if I have the main outlook window on
the primary display.

I think the problem is that I am not setting the form's parent
properly (actually, I'm not setting it all because I'm having trouble
finding out how to do this properly through code period). Here's some
sample code of what I'm doing (from within a class module method):

Dim frmTest as New FormTest

' set some form properties before display
frmTest.Foo = "bar"
frmTest.Baz = "foo"

frmTest.Show vbModal


TIA,
J.P.
 
J

J.P.

I thought it may be worth mentioning that this is a UserForm as
opposed to an Outlook custom form.
 
J

J.P.

I've tried a few solutions with SetParent and SetWindowLong... and I'm
stumped. Any help would be greatly, immensely even, apperciated.
Here's what I've done in the UserForm_Activate() event (closest I
could find to a form load event).

hwnd = FindWindow("ThunderDFrame", Me.Caption)
If (hwnd <> 0) Then
'SetParent hwnd, FindWindow("rctrl_renwnd32", vbNullString)
SetWindowLong hwnd, GWL_HWNDPARENT, FindWindow("rctrl_renwnd32",
vbNullString)
SetForegroundWindow hwnd
Else
MsgBox "Couldn't find the window handle to fix the outlook modal
dialog bug!", vbInformation, Me.Caption
End If


The commented SetParent() call causes the code to hang.
I know that I'm getting the right window handles as well by checking
against the results of GetWindowText() on the handles.
 
J

J.P.

After two days of messing with this I've found a solution to my
problem. Here it is for anyone else who runs into this issue:

Added this public method to my form and called it before the Show
method:

Public Sub FixModalDialogBug()
' Make sure the outlook window is disabled while this dialog is open
Dim hwnd As Long

hwnd = FindWindow("ThunderDFrame", Me.Caption)
If (hwnd <> 0) Then
SetWindowLong hwnd, GWL_HWNDPARENT, FindWindow("rctrl_renwnd32",
vbNullString)
SetForegroundWindow hwnd
Else
MsgBox "Couldn't find the window handle to fix the outlook modal
dialog bug!", vbInformation, Me.Caption
End If

' Center this dialog over the outlook window
CenterMeOverOutlook
End Sub

Public Sub CenterMeOverOutlook()
Dim olWidth, olHeight As Integer

olWidth = Outlook.Application.ActiveExplorer().Width
olHeight = Outlook.Application.ActiveExplorer().Height

Me.Left = (olWidth / 2) - (Me.Width / 2)
Me.Top = (olHeight / 2) - (Me.Height / 2)
End Sub


You need these declarations for the win32 api calls:

Private Declare Function SetForegroundWindow Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetWindowLong Lib "user32" Alias
"SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
As Long

Private Const GWL_HWNDPARENT = (-8)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top