Solved

How can I get VB .NET to open and activate OUTLOOK.EXE?

Posted on 2010-09-03
10
1,152 Views
Last Modified: 2012-05-10
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).MainWindowHandle)
            SetForegroundWindow(Outlook(0).MainWindowHandle)
            Return True
        Else
            Return False
        End If

I would appreciate help getting my form to open/activate Outlook in any circumstance.

Thanks.
0
Comment
Question by:milesryoung
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 42

Expert Comment

by:dqmq
ID: 33597124
VB is not my specialty, but can't you just use the shell() function:

http://msdn.microsoft.com/en-us/library/xe736fyk(VS.71).aspx
0
 
LVL 11

Expert Comment

by:ladarling
ID: 33597841
The Shell function is not .NET managed, use Process.Start:
System.Diagnostics.Process.Start("Outlook.exe")
 
0
 
LVL 11

Expert Comment

by:ladarling
ID: 33597889
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
 
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 33598840
Try something like this:

    FocusApp("Outlook")

Using this code:
    Private Const SW_SHOWNORMAL As Integer = 1



    Private Declare Function IsIconic Lib "user32" (ByVal handle As Integer) As Integer

    Private Declare Function ShowWindow Lib "user32" (ByVal handle As IntPtr, ByVal nCmdShow As Integer) As Integer

    Private Declare Function SetForegroundWindow Lib "user32" (ByVal handle As IntPtr) As Integer



    Private Sub FocusApp(ByVal ExeName As String)

        Dim processes() As Process = Process.GetProcessesByName(ExeName)

        If processes.Length = 0 Then

            Process.Start(ExeName)

        Else

            ' Restore the window if it is minimized...

            If IsIconic(processes(0).MainWindowHandle) <> 0 Then

                ShowWindow(processes(0).MainWindowHandle, SW_SHOWNORMAL)

            End If



            ' Make it the foreground window...

            SetForegroundWindow(processes(0).MainWindowHandle)

        End If

    End Sub

Open in new window

0
 
LVL 11

Expert Comment

by:ladarling
ID: 33599153
@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.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33599286
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.
0
 

Author Comment

by:milesryoung
ID: 33610506
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.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 33611635
"...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?
0
 

Author Comment

by:milesryoung
ID: 33616028
Yes that's right. It is still a running process but the problem is with this line:

SetForegroundWindow(processes(0).MainWindowHandle)

...because Outlook has a MainWindowHandle of "0".
0
 

Author Closing Comment

by:milesryoung
ID: 33617109
I had to use the "Process.Start" in two places but otherwise this was a complete solution.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now