Split Chrome and Excel windows side by side

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi Guys,
I use this code to split the IE browser and excel file windows side by side. Is it possible to change the IE frame to Chrome instead !

VBA Code:
Option Explicit

Private Type RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type


Private Declare PtrSafe Function GetDesktopWindow Lib "user32" () As Long
Private Declare PtrSafe Function GetWindowRect Lib "user32" ( _
    ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _
    ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Declare PtrSafe Function MoveWindow Lib "user32" ( _
    ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, _
    ByVal nHeight As Long, ByVal bRepaint As Long) As Long
Private Declare PtrSafe Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long

Sub SideBySide()
  Dim hwnd As Long
  Dim R As RECT, LW As RECT, RW As RECT

  'Get the size of the deskop
  If GetWindowRect(GetDesktopWindow, R) = 0 Then Exit Sub
  'Calculate the left and right side
  LW = R
  LW.Right = R.Left + (R.Right - R.Left) / 2
  RW = R
  RW.Left = R.Right - (R.Right - R.Left) / 2

  'Move Excel to the Right
  hwnd = FindWindow("XLMAIN", vbEmpty)
  With RW
    MoveWindow hwnd, .Left, .Top, .Right - .Left, .Bottom - .Top, True
  End With
  BringWindowToTop hwnd

  'Move IE to the Left
  hwnd = FindWindow("IEFrame", vbEmpty)
  With LW
    MoveWindow hwnd, .Left, .Top, .Right - .Left, .Bottom - .Top, True
  End With
  BringWindowToTop hwnd
End Sub
 
Hi. No, it wasn't designed to. Basically, the connecting thread between Chrome, Edge and Opera is that they are each based on the Chromium browser. As a result, they each share a certain characteristic (called a class name). It's on that basis that Jaafar's code is able to identify these specific browsers windows and distinguish them from all the other windows that happen to be open on a computer. Firefox is not based on Chromium, and so isn't dealt with in the solution.

Do you think it would be useful to add Firefox?
 
Upvote 0

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi. No, it wasn't designed to. Basically, the connecting thread between Chrome, Edge and Opera is that they are each based on the Chromium browser. As a result, they each share a certain characteristic (called a class name). It's on that basis that Jaafar's code is able to identify these specific browsers windows and distinguish them from all the other windows that happen to be open on a computer. Firefox is not based on Chromium, and so isn't dealt with in the solution.

Do you think it would be useful to add Firefox?
Hi, I searched a bit before but found no luck to get the answer. It would be interesting to learn how the script should be. ?
 
Upvote 0
Ok, well if you're interested in learning what the script would look like, have you read the current version of the script? Does it make sense or do you have any questions about it?

The class name for FireFox is MozillaWindowClass - what do you think would need to change about the current script to make it work with Firefox instead?
 
Upvote 0
So this week is looking like it will be painfully busy, which means I'll likely forget to check back in to the forum. So I'm just going to jump in now and post the updated code, rather than completely forget all about it (which is entirely plausible). In any event, I may have gone a little overboard - a friend of mine jokingly suggested I add more browsers... so I did. It was easy enough to do because most of them are Chromium-based.

Firefox was slightly trickier given the method used to identify the browser; whereas all the other browsers use a hyphen character to separate the browser name from the website title, Firefox uses something that looks like a hyphen (Chr(45)) , but is not (Chr(151))! Just thought I'd throw that tidbit of info in for any ASCII / ANSI / Unicode enthusiasts.

The following code now covers: Brave, Chrome, Edge, FireFox, Internet Explorer, Opera and Vivaldi. Also, SideBySide now takes one of the browsers as an optional argument, meaning that you can start the code by calling it as follows:

VBA Code:
SideBySide FireFox
SideBySide Opera

An example - UseBrowser - is included below. If you omit the browser, and just call the subroutine SideBySide, it will default to Chrome. I did this to save you from having to go in and edit the code each time you wanted to change the browser selection (and it also made it easier for me to test!). I've added an extra comment here and there, but let us know if you have any questions.

VBA Code:
Option Explicit

Enum BrowsersEnum
    [_Default]
    Brave
    Chrome
    Edge
    FireFox
    InternetExplorer
    Opera
    Vivaldi
End Enum

Private Type RECT
  Left As Long
  Top As Long
  Right As Long
  Bottom As Long
End Type

Private Type POINTAPI
  x As Long
  y As Long
End Type

Private Type WINDOWPLACEMENT
  Length As Long
  flags As Long
  showCmd As Long
  ptMinPosition As POINTAPI
  ptMaxPosition As POINTAPI
  rcNormalPosition As RECT
End Type

Private Type GUID
    Data1 As Long
    Data2 As Integer
    Data3 As Integer
    Data4(0 To 7) As Byte
End Type

#If VBA7 Then
    Private Declare PtrSafe Function SetWindowPlacement Lib "user32" (ByVal hwnd As LongPtr, ByRef lpwndpl As WINDOWPLACEMENT) As Long
    Private Declare PtrSafe Function GetDesktopWindow Lib "user32" () As LongPtr
    Private Declare PtrSafe Function GetWindowRect Lib "user32" (ByVal hwnd As LongPtr, lpRect As RECT) As Long
    Private Declare PtrSafe Function MoveWindow Lib "user32" (ByVal hwnd As LongPtr, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
    Private Declare PtrSafe Function BringWindowToTop Lib "user32" (ByVal hwnd As LongPtr) As Long
    Private Declare PtrSafe Function GetTopWindow Lib "user32" (ByVal hwnd As LongPtr) 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 AccessibleObjectFromWindow Lib "OLEACC.DLL" (ByVal hwnd As LongPtr, ByVal dwId As Long, ByVal riid As LongPtr, ppvObject As Any) As Long
    Private Declare PtrSafe Function IIDFromString Lib "ole32.dll" (ByVal lpsz As LongPtr, ByVal lpiid As LongPtr) As LongPtr
    Private Declare PtrSafe Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As LongPtr, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
#Else
    Private Declare Function SetWindowPlacement Lib "user32" (ByVal hwnd As Long, ByRef lpwndpl As WINDOWPLACEMENT) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long
    Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetTopWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetNextWindow Lib "user32" Alias "GetWindow" (ByVal hwnd As Long, ByVal wFlag As Long) As Long
    Private Declare Function AccessibleObjectFromWindow Lib "OLEACC.DLL" (ByVal hwnd As Long, ByVal dwId As Long, ByVal riid As Long, ppvObject As Any) As Long
    Private Declare Function IIDFromString Lib "ole32.dll" (ByVal lpsz As Long, ByVal lpiid As Long) As Long
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
#End If

Sub UseBrowser()

    SideBySide FireFox

End Sub

Sub SideBySide(Optional SelectedBrowser As BrowsersEnum)

#If Win64 Then
    Dim hwnds() As LongLong
#Else
    Dim hwnds() As Long
#End If
 
    Const SW_SHOWNORMAL As Long = 1
    Dim hwnd As Variant
    Dim R As RECT, LW As RECT, RW As RECT, WP As WINDOWPLACEMENT
 
    ' Get the size of the deskop
    If GetWindowRect(GetDesktopWindow, R) = 0 Then Exit Sub
    ' Calculate the left and right side
    LW = R
    LW.Right = R.Left + (R.Right - R.Left) / 2
    RW = R
    RW.Left = R.Right - (R.Right - R.Left) / 2
 
    ' Move Excel to the right
    hwnd = Application.hwnd
    With RW
        Call MoveWindow(hwnd, .Left, .Top, .Right - .Left, .Bottom - .Top, True)
    End With
    Call BringWindowToTop(hwnd)
    
    If SelectedBrowser = Default Then SelectedBrowser = Chrome
 
    ' Get the hwnds of all targeted browsers
    hwnds = GetInternetBrowsersHwnd(SelectedBrowser) '<= pass the selected browser from the available enums
 
    If Not Not hwnds Then
        For Each hwnd In hwnds
            ' Ensure that the Window is set to normal mode
            WP.Length = Len(WP)
            WP.showCmd = SW_SHOWNORMAL
            Call SetWindowPlacement(hwnd, WP)
            ' Use the Window Handle to move the browser window to the left hand side of the screen
            With LW
                Call MoveWindow(hwnd, .Left, .Top, .Right - .Left, .Bottom - .Top, True)
            End With
            Call BringWindowToTop(hwnd)
        Next
    Else
        Debug.Print "NB: No specified browser windows were found found!"
    End If
End Sub

#If Win64 Then
    Private Function GetInternetBrowsersHwnd(ByVal Browser As BrowsersEnum) As LongLong()
    Dim hwnd As LongLong, hwnds() As LongLong
#Else
    Private Function GetInternetBrowsersHwnd(ByVal Browser As BrowsersEnum) As Long()
    Dim hwnd As Long, hwnds() As Long
#End If

    Const GW_HWNDNEXT = 2
    Dim sClassName As String * 256, lRet As Long
    Dim sBrowser As String, sAccName As String
    Dim HwndIndex As Long
    Dim WindowClassName As String
    Dim BrowserClasses As String
   
    BrowserClasses = "Chrome_WidgetWin_1|IEFrame|MozillaWindowClass"
   
    ' Each of the window captions comprise the title of the current website together with the browser name, delimited by a hyphen
    ' character. The hyphen character used is Chr(45), save for Mozilla Firefox, which uses Chr(151).
    sBrowser = Switch(Browser = Edge, "- Microsoft Edge", Browser = Opera, "- Opera", Browser = Chrome, "- Google Chrome", Browser = InternetExplorer, "- Internet Explorer", Browser = FireFox, Chr(151) & " Mozilla FireFox", Browser = Vivaldi, "- Vivaldi", Browser = Brave, "- Brave")
    hwnd = GetTopWindow(0)
    Do While hwnd <> 0
        lRet = GetClassName(hwnd, sClassName, 256)
        WindowClassName = Left(sClassName, lRet)
        If InStr(BrowserClasses, WindowClassName) > 0 Then
            sAccName = GetBrowserAccName(hwnd)
            If InStr(1, sAccName, sBrowser, vbTextCompare) Then
                ReDim Preserve hwnds(HwndIndex)
                hwnds(HwndIndex) = hwnd
                HwndIndex = HwndIndex + 1
            End If
        End If
        hwnd = GetNextWindow(hwnd, GW_HWNDNEXT)
    Loop
    GetInternetBrowsersHwnd = hwnds

End Function


#If Win64 Then
    Private Function GetBrowserAccName(ByVal hwnd As LongLong) As String
#Else
    Private Function GetBrowserAccName(ByVal hwnd As Long) As String
#End If

    Const ID_ACCESSIBLE As String = "{618736E0-3C3D-11CF-810C-00AA00389B71}"
    Const OBJID_CLIENT = &HFFFFFFFC
    Const S_OK = &H0

    Dim tGUID(0 To 3) As Long
    Dim oIAc As IAccessible
 
    If IIDFromString(StrPtr(ID_ACCESSIBLE), VarPtr(tGUID(0))) = S_OK Then
        If AccessibleObjectFromWindow(hwnd, OBJID_CLIENT, VarPtr(tGUID(0)), oIAc) = S_OK Then
           GetBrowserAccName = oIAc.accName(0&)
        End If
    End If

End Function
 
Upvote 0
Not to steal anyone's thunder, but here's an approach using stdVBA's stdWindow, stdLambda and stdICallable classes.

VBA Code:
Sub SideBySide()
  Dim desktop As stdWindow: Set desktop = stdWindow.CreateFromDesktop()
  With stdWindow.CreateFromApplication()
    .State = Normal
    .x = desktop.x
    .y = desktop.y
    .width = desktop.width / 2
    .height = desktop.height - 40
  End With
  With stdWindow.CreateFromDesktop.FindFirst(stdLambda.Create("if $2 > 1 then EWndFindResult.NoMatchSkipDescendents else $1.Class = ""Chrome_WidgetWin_1"" And $1.Caption like ""*- Google Chrome"""))
    .State = Normal
    .x = desktop.x + desktop.width / 2
    .y = desktop.y
    .width = desktop.width / 2
    .height = desktop.height - 40
    .Activate
  End With
End Sub
 
Upvote 0
Hi Dan_W,
The new code works all browsers except Firefox !
 

Attachments

  • Screenshot 2022-04-05 161241.png
    Screenshot 2022-04-05 161241.png
    45.2 KB · Views: 4
Upvote 0
Not to steal anyone's thunder, but here's an approach using stdVBA's stdWindow, stdLambda and stdICallable classes.

VBA Code:
Sub SideBySide()
  Dim desktop As stdWindow: Set desktop = stdWindow.CreateFromDesktop()
  With stdWindow.CreateFromApplication()
    .State = Normal
    .x = desktop.x
    .y = desktop.y
    .width = desktop.width / 2
    .height = desktop.height - 40
  End With
  With stdWindow.CreateFromDesktop.FindFirst(stdLambda.Create("if $2 > 1 then EWndFindResult.NoMatchSkipDescendents else $1.Class = ""Chrome_WidgetWin_1"" And $1.Caption like ""*- Google Chrome"""))
    .State = Normal
    .x = desktop.x + desktop.width / 2
    .y = desktop.y
    .width = desktop.width / 2
    .height = desktop.height - 40
    .Activate
  End With
End Sub
Hi, the code does not work through.
 

Attachments

  • Screenshot 2022-04-05 161633.png
    Screenshot 2022-04-05 161633.png
    11.9 KB · Views: 7
Upvote 0
@sancarn can you please post all the relevant code, rather than just to another site, as per Rule#4
Thanks
 
Upvote 0
@sancarn can you please post all the relevant code, rather than just to another site, as per Rule#4
Thanks
Hi fluff, I get the point, but stdVBA framework is pretty large. Do you really want me to post all 1800 lines of stdLambda and all 2200 lines of stdWindow in my answer? I would be surprised if this site even allows me to post so much data in a single post.
 
Upvote 0

Forum statistics

Threads
1,215,066
Messages
6,122,947
Members
449,095
Latest member
nmaske

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