milesryoung
asked on
How can I get VB .NET to open and activate OUTLOOK.EXE?
Hi,
I am currently creating a small application to display the contents of a user's inbox in Outlook. Double clicking the only window should open and activate Outlook.
All is working as expected when Outlook is actually open but if I close it or minimise it (despite the process still running in the background), I can't seen to get my application to make Outlook visible and make it the active window.
This code only works when Outlook is currently visible and behind other windows:
Declare Auto Function SetForegroundWindow Lib "USER32.DLL" _
(ByVal hWnd As IntPtr) As Boolean
Declare Function SetActiveWindow Lib "USER32.DLL" (ByVal hwnd As Long) As Long
Dim Outlook() As Process = Process.GetProcessesByName ("Outlook" )
If Outlook.Length > 0 Then
SetActiveWindow(Outlook(0) .MainWindo wHandle)
SetForegroundWindow(Outloo k(0).MainW indowHandl e)
Return True
Else
Return False
End If
I would appreciate help getting my form to open/activate Outlook in any circumstance.
Thanks.
I am currently creating a small application to display the contents of a user's inbox in Outlook. Double clicking the only window should open and activate Outlook.
All is working as expected when Outlook is actually open but if I close it or minimise it (despite the process still running in the background), I can't seen to get my application to make Outlook visible and make it the active window.
This code only works when Outlook is currently visible and behind other windows:
Declare Auto Function SetForegroundWindow Lib "USER32.DLL" _
(ByVal hWnd As IntPtr) As Boolean
Declare Function SetActiveWindow Lib "USER32.DLL" (ByVal hwnd As Long) As Long
Dim Outlook() As Process = Process.GetProcessesByName
If Outlook.Length > 0 Then
SetActiveWindow(Outlook(0)
SetForegroundWindow(Outloo
Return True
Else
Return False
End If
I would appreciate help getting my form to open/activate Outlook in any circumstance.
Thanks.
The Shell function is not .NET managed, use Process.Start:
System.Diagnostics.Process .Start("Ou tlook.exe" )
System.Diagnostics.Process
If your application needs to 'own' that instance of outlook, you may want to declare it as a Process object in your app so that you can manipulate it for the eintire lifecycle of the outlook window:
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
@Idle: I wonder why they dont have a managed SetForeground method for the Process class? They obviously anticipated people wanting to interact with the app main window, why not add methods to do what we are trying to do. Cross-app communication hasnt really evolved much since the DDE concept, its kinda lame.
I agree...they only went halfway with the legacy AppActivate() function:
http://msdn.microsoft.com/en-us/library/dyz95fhy(VS.80).aspx
"The AppActivate function changes the focus to the named application or window but does not affect whether it is maximized or minimized."
I think part of the problem is that there are so many different libraries out there with controls/windows that don't support standard windows messages and then what constitutes a "window" keeps evolving...
...in WPF, for example, the "handle" of a window is hidden even deeper by default requiring even more work to obtain it.
http://msdn.microsoft.com/en-us/library/dyz95fhy(VS.80).aspx
"The AppActivate function changes the focus to the named application or window but does not affect whether it is maximized or minimized."
I think part of the problem is that there are so many different libraries out there with controls/windows that don't support standard windows messages and then what constitutes a "window" keeps evolving...
...in WPF, for example, the "handle" of a window is hidden even deeper by default requiring even more work to obtain it.
ASKER
Hi,
Thank you to all for your input. @Idle_Mind, I've tried your code but although it now works when Outlook is minimised, it still doesn't work if Outlook has been closed (although the process is still running).
In this case the SetForegroundWindow function gets "0" as the MainWindowHandle.
Thank you to all for your input. @Idle_Mind, I've tried your code but although it now works when Outlook is minimised, it still doesn't work if Outlook has been closed (although the process is still running).
In this case the SetForegroundWindow function gets "0" as the MainWindowHandle.
"...it still doesn't work if Outlook has been closed (although the process is still running)."
Hmm...so when you "close" Outlook with the 'X' in the top right it stays in the background?
If you just run it again every time maybe it will bring up the main window?
Hmm...so when you "close" Outlook with the 'X' in the top right it stays in the background?
If you just run it again every time maybe it will bring up the main window?
ASKER
Yes that's right. It is still a running process but the problem is with this line:
SetForegroundWindow(proces ses(0).Mai nWindowHan dle)
...because Outlook has a MainWindowHandle of "0".
SetForegroundWindow(proces
...because Outlook has a MainWindowHandle of "0".
ASKER
I had to use the "Process.Start" in two places but otherwise this was a complete solution.
http://msdn.microsoft.com/en-us/library/xe736fyk(VS.71).aspx