VBA - Any way to cycle between excel window and browser window?

szita2000

Board Regular
Joined
Apr 25, 2012
Messages
101
Office Version
  1. 365
Platform
  1. Windows
Hi guys.

I need to display one of my excel file on a big screen, but also need to show some data from our internal website. (In the website format, so no. Data connection is not an option)
I tried messing with the Set objShell = CreateObject("Shell.Application"), but I could only find method for .MinimizeAll.
That did minimize all windows, but that's not what I want.

The excel workbook will have multiple windows open from the same workbook, and I need to cycle between the excel app (with all of it's windows together) and the chrome browser window.

Is this possible?

Thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try this and see if it works for you.

Basically the code should loop through each window of the workbook and bring each to the front screen for 2 seconds then it should bring the chrome browser to the front also for 2 seconds and finally it should take you back to the initial window... Set the MaximizedState Optional argument if you want to display the windows maximized.

Code in a Standard Module and run the Start routine:
Code:
Option Explicit

[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
    Private Declare PtrSafe Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As LongPtr, ByVal wFlag As Long) As LongPtr
    Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
    Private Declare PtrSafe Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
    Private Declare PtrSafe Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As LongPtr, lpdwProcessId As Long) As Long
    Private Declare PtrSafe Function GetForegroundWindow Lib "user32" () As LongPtr
    Private Declare PtrSafe Function IsIconic Lib "user32" (ByVal hwnd As LongPtr) As Long
    Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal nCmdShow As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If


Sub Start()

    Application.OnTime Now, "'DisplayWindows " & True & "'"

End Sub


Sub DisplayWindows(Optional ByVal MaximizedState As Boolean = True)

    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
        Dim hwnd As LongPtr
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
        Dim hwnd As Long
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If
    
    Dim oActiveWindow  As Window, oWnd As Window
    
    ActiveWindow.Activate
    Set oActiveWindow = ActiveWindow

    For Each oWnd In ThisWorkbook.Windows
        oWnd.Activate
        If MaximizedState Then oWnd.WindowState = xlMaximized
        Delay 2
    Next oWnd
    
    hwnd = FindWindow("Chrome_WidgetWin_1", vbNullString)
    If hwnd Then
        Call BringWindowToFront(hwnd, MaximizedState)
        Delay 2
    End If
    
    VBA.AppActivate Application.Caption
    oActiveWindow.Activate
    
    MsgBox "Back to initial window." & vbCrLf & vbCrLf & "Done!", vbInformation
    
End Sub


Private Sub Delay(DelayTime As Single)

    Dim sngTimer As Single
    
    sngTimer = Timer
    Do
        DoEvents
    Loop Until Timer - sngTimer >= DelayTime

End Sub


Private Sub BringWindowToFront(ByVal hwnd As LongPtr, Optional ByVal MaximizedState As Boolean = True)
 
    Const SW_SHOW = 5
    Const SW_SHOWMAXIMIZED = 3
    Const SW_RESTORE = 9
    Const GW_OWNER = 4
    
    Dim lThreadID1 As Long, lThreadID2 As Long
    
    On Error Resume Next
    
    If hwnd <> GetForegroundWindow() Then
        lThreadID1 = GetWindowThreadProcessId(GetForegroundWindow, ByVal 0&)
        lThreadID2 = GetWindowThreadProcessId(hwnd, ByVal 0&)
        Call AttachThreadInput(lThreadID1, lThreadID2, True)
        Call SetForegroundWindow(hwnd)
        If IsIconic(GetNextWindow(hwnd, 4)) Then
            Call ShowWindow(GetNextWindow(hwnd, GW_OWNER), IIf(MaximizedState, SW_SHOWMAXIMIZED, SW_RESTORE))
        Else
            Call ShowWindow(GetNextWindow(hwnd, GW_OWNER), IIf(MaximizedState, SW_SHOWMAXIMIZED, SW_SHOW))
        End If
        DoEvents
        Call AttachThreadInput(lThreadID2, lThreadID1, True)
    End If
End Sub

Change the Delay Time as required.
 
Upvote 0
Solution
Hi Jaafar! Thanks very much for the solution.
I will try to implement it today.
 
Upvote 0
Try this and see if it works for you.

Basically the code should loop through each window of the workbook and bring each to the front screen for 2 seconds then it should bring the chrome browser to the front also for 2 seconds and finally it should take you back to the initial window... Set the MaximizedState Optional argument if you want to display the windows maximized.

Code in a Standard Module and run the Start routine:
Code:
Option Explicit

[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
    Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
    Private Declare PtrSafe Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As LongPtr, ByVal wFlag As Long) As LongPtr
    Private Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hwnd As LongPtr) As Long
    Private Declare PtrSafe Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
    Private Declare PtrSafe Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As LongPtr, lpdwProcessId As Long) As Long
    Private Declare PtrSafe Function GetForegroundWindow Lib "user32" () As LongPtr
    Private Declare PtrSafe Function IsIconic Lib "user32" (ByVal hwnd As LongPtr) As Long
    Private Declare PtrSafe Function ShowWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal nCmdShow As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
    Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
    Private Declare Function GetForegroundWindow Lib "user32" () As Long
    Private Declare Function IsIconic Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If


Sub Start()

    Application.OnTime Now, "'DisplayWindows " & True & "'"

End Sub


Sub DisplayWindows(Optional ByVal MaximizedState As Boolean = True)

    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
        Dim hwnd As LongPtr
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL]
        Dim hwnd As Long
    [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If
   
    Dim oActiveWindow  As Window, oWnd As Window
   
    ActiveWindow.Activate
    Set oActiveWindow = ActiveWindow

    For Each oWnd In ThisWorkbook.Windows
        oWnd.Activate
        If MaximizedState Then oWnd.WindowState = xlMaximized
        Delay 2
    Next oWnd
   
    hwnd = FindWindow("Chrome_WidgetWin_1", vbNullString)
    If hwnd Then
        Call BringWindowToFront(hwnd, MaximizedState)
        Delay 2
    End If
   
    VBA.AppActivate Application.Caption
    oActiveWindow.Activate
   
    MsgBox "Back to initial window." & vbCrLf & vbCrLf & "Done!", vbInformation
   
End Sub


Private Sub Delay(DelayTime As Single)

    Dim sngTimer As Single
   
    sngTimer = Timer
    Do
        DoEvents
    Loop Until Timer - sngTimer >= DelayTime

End Sub


Private Sub BringWindowToFront(ByVal hwnd As LongPtr, Optional ByVal MaximizedState As Boolean = True)

    Const SW_SHOW = 5
    Const SW_SHOWMAXIMIZED = 3
    Const SW_RESTORE = 9
    Const GW_OWNER = 4
   
    Dim lThreadID1 As Long, lThreadID2 As Long
   
    On Error Resume Next
   
    If hwnd <> GetForegroundWindow() Then
        lThreadID1 = GetWindowThreadProcessId(GetForegroundWindow, ByVal 0&)
        lThreadID2 = GetWindowThreadProcessId(hwnd, ByVal 0&)
        Call AttachThreadInput(lThreadID1, lThreadID2, True)
        Call SetForegroundWindow(hwnd)
        If IsIconic(GetNextWindow(hwnd, 4)) Then
            Call ShowWindow(GetNextWindow(hwnd, GW_OWNER), IIf(MaximizedState, SW_SHOWMAXIMIZED, SW_RESTORE))
        Else
            Call ShowWindow(GetNextWindow(hwnd, GW_OWNER), IIf(MaximizedState, SW_SHOWMAXIMIZED, SW_SHOW))
        End If
        DoEvents
        Call AttachThreadInput(lThreadID2, lThreadID1, True)
    End If
End Sub

Change the Delay Time as required.
Hi Jaafar, this works when the chrome browser already opened. How to make it even better to launch the chrome browser with preset home pages if the browser is yet opened !
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,111
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top