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
 
I was able to recreate this problem on my desktop, which has multiple accounts/profiles set on Google Chrome. This has the effect of changing the browser's caption, which can cause difficulties for code being able to identify it. There should be a message logged in the Immediate Window of the VBA Editor - did you see one?

At any rate, assuming this to be the case with your Chrome, I have slightly adjusted one line of the code and rather than ask you to hunt it down and amend it, I reproduce the code in full below:

VBA Code:
Option Explicit

Private Enum Chromium_Based_Browsers
    Edge
    opera
    Chrome
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 SideBySide()

#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)
  
    ' get the hwnds of all targeted browsers
    hwnds = GetInternetBrowsersHwnd(Chrome) '<= pass chrome, Opera or Edge 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 Chrome 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 Chromium_Based_Browsers) As LongLong()
    Dim hwnd As LongLong, hwnds() As LongLong
#Else
    Private Function GetInternetBrowsersHwnd(ByVal Browser As Chromium_Based_Browsers) 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
  
    sBrowser = Switch(Browser = Edge, "- Microsoft Edge", Browser = opera, "- Opera", Browser = Chrome, "- Google Chrome")
    hwnd = GetTopWindow(0)
    Do While hwnd <> 0
        lRet = GetClassName(hwnd, sClassName, 256)
        If Left(sClassName, lRet) = "Chrome_WidgetWin_1" 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

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi, the new code works on Chrome now but not Opera and Chromium Edge.
 
Upvote 0
Hmm - What does it do, if anything? Was there any message in the Immediate Window?
Did you change this line in the code?

VBA Code:
hwnds = GetInternetBrowsersHwnd(Chrome)

I don't have Opera, but can install it depending on the above.
 
Upvote 0
Sorry - I meant to thank you earlier for taking the time to check - I know you were only looking to get the code working with Chrome, I had completely forgotten that point. Glad it's working for you. :)
 
Upvote 0
Hi Dan, I got your point now. I tested on all three browsers and the code works. Thanks a lot.
 
Upvote 0
That's great to hear, thank you for letting us know.
 
Upvote 0

Forum statistics

Threads
1,214,892
Messages
6,122,112
Members
449,066
Latest member
Andyg666

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