Search code examples
vb.netexcelinternet-explorerwindowforeground

vb.net Change active window between Internet Explorer and Excel


I'm writing a program in VB.NET that opens Excel and Internet Explorer. In order to send keys to this programs, I need to bring the window of one of them to the foreground. And than to the same with the other one several times.

How can I proceed to do that?

Code to open Excel:

xlsApp = New Excel.Application
            xlsApp.Visible = True
            xlsWorkBooks = xlsApp.Workbooks
            xlsWB = xlsWorkBooks.Add() 'Open("T:\Dados_de_Aplicativos\Travel_Finder\texte.xlsx")
            xlsWS = xlsWB.Sheets(1)

Code to open IE:

    Dim IE As SHDocVw.InternetExplorer
    IE = CreateObject("InternetExplorer.Application")

After I open IE, I can put Excel into the foreground doing this:

xlsApp.WindowState = Excel.XlWindowState.xlMinimized
        If xlsApp.WindowState = Excel.XlWindowState.xlMinimized Then
            xlsApp.WindowState = Excel.XlWindowState.xlMaximized
        End If

But I'm not finding similar method to the IE Browser.

Can you please give me an example if there is a solution?


Solution

  • The easiest way would be to use 'AppActivate' using the name of the application or his PID. Something like : AppActivate("Application Title") or AppActivate(PID), but if the window is minimized, it won't work.

    This post How to restore a minimized window using AppActivate(ProcessID) can help you.

    You could also use Pinvoke.