Home » excel » Get Excel Window back to Foreground

Get Excel Window back to Foreground

Posted by: admin May 14, 2020 Leave a comment


I searched the Internet from top to the root, but I couldn’t find a solution. In my Excel Workbook I want to import Contacts from Outlook. This works fine without any problems. I get the GAL to the foreground by using Outlook.ActiveWindow.Activate and I minimize Outlook by using Outlook.Explorers.Item(1).WindowState = olMinimized. I know this is not really a very clean solution, but everything works fine unless there are no other Outlook Windows opened.

Now to my problem:
When the user has more than one opened Outlook Window (e.g. a new mail dialog, etc.) I can’t bring my Excel Application back to Foreground. The import of contacts still works fine. The GAL comes to Foreground by clicking a Button.
I tried the following code snippets, but they don’t really work.

   Dim AppTitle As String
   AppTitle = Application.Caption
   'some code
   AppActivate AppTitle

Another attempt was this function in a module

   Public Declare Function SetForegroundWindow Lib "user32" _
   (ByVal hWnd As Long) As Long

calling it in my Button Function

   Dim AppXL As Object
   Set AppXL = CreateObject("Excel.Application")
   'some code
   SetForegroundWindow AppXL.hWnd

I also tried to use this code snippet

   Excel.Application.Visible = True

Nothing helped. The only thing happened was the blinking Excel Icon in the taskbar. I hope you can help me out with this.

How to&Answers:

Assuming this code is in your Excel workbook, it would just be:

   SetForegroundWindow Application.hWnd

rather than creating a new application instance.