VBA: Get URL of a Chrome browser window

User avatar
SammyB
StarLounger
Posts: 93
Joined: 04 Mar 2010, 16:32

VBA: Get URL of a Chrome browser window

Post by SammyB »

I would like to look at all of the visible windows & if it is a Chrome window, find the URL. Cycling thru the windows is easy, but I'm not sure how to get the URL once I have found a Chrome window. Here is the code to find the Chrome window(s), but I'm not sure what to do next. TIA

Code: Select all

Option Explicit
Private Declare PtrSafe Function apiGetDesktopWindow Lib "user32" Alias _
                "GetDesktopWindow" () As Long
Private Declare PtrSafe Function apiGetWindow Lib "user32" Alias _
                "GetWindow" (ByVal hwnd As Long, _
                ByVal wCmd As Long) As Long
Private Declare PtrSafe Function apiGetWindowText Lib "user32" Alias _
                "GetWindowTextA" (ByVal hwnd As Long, ByVal _
                lpString As String, ByVal aint As Long) As Long
Private Declare PtrSafe Function apiGetWindowLong Lib "user32" Alias _
                "GetWindowLongA" (ByVal hwnd As Long, ByVal _
                nIndex As Long) As Long
                
Private Const GW_CHILD As Long = 5
Private Const GW_HWNDNEXT As Long = 2
Private Const GWL_STYLE As Long = (-16)
Private Const mconMAXLEN = 255

Public Sub ChromeInfo()
    Dim hwnd As Long, sWinTxt As String, iLength As Long, iHandleStyle As Long
    Dim v As Variant
    hwnd = apiGetWindow(apiGetDesktopWindow(), GW_CHILD)
    Do While hwnd <> 0
        sWinTxt = String$(mconMAXLEN - 1, 0)
        iLength = apiGetWindowText(hwnd, sWinTxt, mconMAXLEN)
        If iLength > 0 Then
            sWinTxt = left$(sWinTxt, iLength)
            iHandleStyle = apiGetWindowLong(hwnd, GWL_STYLE)
            If iHandleStyle And WS_VISIBLE Then
                v = Split(sWinTxt, "-")
                If v(UBound(v)) = " Google Chrome" Then
                    MsgBox "Need to get Chrome URL for " & v(0)
                End If
            End If
        End If
        hwnd = apiGetWindow(hwnd, GW_HWNDNEXT)
    Loop
End Sub

User avatar
HansV
Administrator
Posts: 78493
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA: Get URL of a Chrome browser window

Post by HansV »

Sorry, I can only find old code that doesn't work with recent versions of Chrome... :sorry:

You might have a look at Selenium.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: VBA: Get URL of a Chrome browser window

Post by YasserKhalil »

Can you share the code that you found?

User avatar
HansV
Administrator
Posts: 78493
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: VBA: Get URL of a Chrome browser window

Post by HansV »

Best wishes,
Hans

User avatar
ChrisGreaves
PlutoniumLounger
Posts: 15628
Joined: 24 Jan 2010, 23:23
Location: brings.slot.perky

Re: VBA: Get URL of a Chrome browser window

Post by ChrisGreaves »

YasserKhalil wrote:
20 May 2022, 08:18
Can you share the code that you found?
i found the WiseOwl videos useful.
The videos are written in Excel/VBA but I adapted them for Word20002/VBA with success.
This Topic too may help.
Cheers, Chris
There's nothing heavier than an empty water bottle

User avatar
SammyB
StarLounger
Posts: 93
Joined: 04 Mar 2010, 16:32

Re: VBA: Get URL of a Chrome browser window

Post by SammyB »

Sorry that I am so long in responding. I'm too busy on other stuff. I will eventually look at the links that everyone provided. I think that the bottom line is to have two macros. It is for a window manager that knows exactly where I want to see my windows, but a Chrome window for Fathom needs to be just a little wider than I would normally have, so rather than try to understand Selenium, it will probably just be easier to have an extra button for a Fathom window setup because I only use it a couple of days a month. But, I will eventually give an integrated approach another try. Thanks!