Open browser, send a few URLs, then close window?

TheDougmeister

New Member
Joined
Oct 12, 2021
Messages
25
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Programmatically, how can I close a browser window that I have opened from within Microsoft Access?

I am currently using the "Shell" command but am open to using another method.

P.S. I tried setting a variable ("pHandle") as the return value of the Shell command, then using some code I found online to search for that and close it (set it to "nothing"), but it didn't work.
 
I took everything else out of my program and this is the bare bones. It still exhibits the behavior the same as before (only Vivaldi closes).

VBA Code:
Option Compare Database
Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Dim pHandle As Variant

Public Sub cmdStart_Click()

pHandle = 1
OpenNotepad (pHandle)

End Sub


Sub OpenNotepad(pHandle)
    
    Debug.Print pHandle

    pHandle = Shell("C:\Program Files (x86)\BraveSoftware\Brave-Browser\Application\brave.exe", vbNormalFocus)
    Debug.Print "Brave handle = " & pHandle
    Call WaitSeconds(10)
    CloseProgram pHandle

    pHandle = Shell("C:\Program Files (x86)\Mozilla Firefox\firefox.exe") ', vbNormalFocus)
    Debug.Print "Firefox handle = " & pHandle
    Call WaitSeconds(10)
    CloseProgram pHandle
    
    pHandle = Shell("D:\Program Files\Vivaldi\Application\vivaldi.exe", vbNormalFocus)
    Debug.Print "Vivaldi handle = " & pHandle
    Call WaitSeconds(10)
    CloseProgram pHandle
    
    pHandle = Shell("D:\Program Files\Opera\launcher.exe", vbNormalFocus)
    Debug.Print "Opera handle = " & pHandle
    Call WaitSeconds(10)
    CloseProgram pHandle
    
    pHandle = Shell("C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", vbNormalFocus)
    Debug.Print "Edge handle = " & pHandle
    Call WaitSeconds(10)
    CloseProgram pHandle
    
End Sub

Sub CloseProgram(TargetPHandle As Variant)
    
    Shell "TaskKill /F /PID " & TargetPHandle, vbHide
    Debug.Print TargetPHandle & " killed"
    
End Sub

Public Sub WaitSeconds(Seconds)

On Error GoTo PROC_ERR
Dim datTime As Date

  Seconds = Int(Seconds)
  datTime = DateAdd("s", Seconds, Now)
  
  Do
    Sleep 100    ' Yield to other programs (better than using DoEvents which eats up all the CPU cycles)
    DoEvents
  Loop Until Now >= datTime

PROC_EXIT:
  Exit Sub

PROC_ERR:
  MsgBox "Error: " & Err.Number & ". " & Err.Description, , "modDateTime.WaitSeconds"
  Resume PROC_EXIT
End Sub
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Hi
So I tried it on my system, and I experienced the same-ish issue, but only with Firefox, and not the other browsers (curiously). I tried doing the whole thing manually, and even after starting the browser with shell, the instance (of Firefox) to which the handle referred instantly seemed to disappear. I'll need to look into it in more detail.

Looking at your code, though, a bit closer, I noticed:
pHandle = Shell("C:\Program Files (x86)\Mozilla Firefox\firefox.exe") ', vbNormalFocus)
You seem to have commented out the "vbNormalFocus" part - the consequence of doing this is that it will execute the program with vbHide. This means that it will actually run, but you just won't see it happen... nor will you then see it close down. So try putting that back to normal just for testing.

Also, with the WaitSeconds routine:
"
VBA Code:
  Do
    Sleep 100    ' Yield to other programs (better than using DoEvents which eats up all the CPU cycles)
    DoEvents
  Loop Until Now >= datTime
"
Can you please delete the Sleep line. It might yield to other programs, but at the moment, you're principally concerned with getting Access to operate correctly, and this will put a pause on Access operating. I'll take another look at what's going on the handles, and consider how best to close the process over the weekend.
 
Upvote 0
Thanks again for looking into this with me.

I made the two (2) changes that you suggested. Same results.

Well, almost.

And this *really* bothers me... Vivaldi no longer closes. The mystery deepens...
 
Upvote 0
Hi - apologies for the radio silence. I managed to find time to look at this some more over the weekend, and somewhat unhelpfully (this is an understatement), many of the browsers appear to change their process IDs upon startup.

Another way of pinpointing a specific program is to get the window's handle (hWnd) using an API which is not especially difficult, save that it would require you to also specify the caption of that program. When it comes to browsers, the caption almost always starts with the name of the website that the current tab is open to and while that might be possible in your particular circumstances, it's far from ideal. An alternative to that is to cycle through all available window handles, and compare them to a partial caption - here the partial caption would be the name of the browser; if it's a match, then add it to the list of target handles. Once you've assembled your list of target handles, you could use another API to go through the list and close each of those windows/browsers down. The issue with that is that it wouldn't differentiate between a browser type (e.g. Firefox) that is already open on your system, and the instance of Firefox that you're starting up for the purpose of your specific task. This is also less than ideal, but I've yet to come up with an alternative method of doing this.

What I'm thinking though is - before instantiating a new browser, first take a 'snapshot' (ie., make a list) of all window handles that match that browser type, then start the browser, then take another snapshot of all window handles that match the given browser type, compare the two, and any handles in the second list that are not in the first must be the new browser instances, and then proceed to terminate those instances.

It sounds very involved and coding-wise it kinda is (as compared to the above), but it's not onerous at all because I have the bits and pieces from over things I've written over the past few years. Just for my context though, can you give me a little bit more information about what it is that you're doing/trying to accomplish? Also, which browsers are you looking to use? Is it only those that you listed in your sample code the other day?
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
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