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.
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Setting the variable to nothing won't close the program, it simply sets the value of the variable to nothing.

The example - OpenNotepad - below will use Shell to open Notepad, retaining the Process ID in the variable pHandle. It then stops running the code, to demonstrate to you that it has opened, and then if you press F5 to continue it executes the CloseProgram using the Process ID in the pHandle variable. Without seeing your code, it's difficult to say how to get this to work for you, but basically, you just want to pass the number in the pHandle variable from when you open the browser to the CloseProgram subroutine, as demonstrated below.

VBA Code:
Sub OpenNotepad()
    
    Dim pHandle As Variant
    
    pHandle = Shell("Notepad.exe", vbNormalFocus)
    Stop
    CloseProgram pHandle

End Sub

Sub CloseProgram(TargetPHandle As Variant)
    
    Shell "TaskKill /F /PID " & TargetPHandle, vbHide

End Sub

Hope that helps.
 
Upvote 0
Thanks for your reply.

I tried it as follows, with a few minor changes, but the browser that I opened did not close.

1) ' I open a "clean" window first

VBA Code:
' sBrowser is the full path to the browser executable
' strNewWindow is a browser-dependent parameter that forces the browser to open in a new window (not just a new tab in an existing browser window)
' pHandle is DIM'ed as type Variant
Call LaunchProcess(sCommandString, pHandle)

Public Sub LaunchProcess(sCommandString, pHandle)
    pHandle = Shell(sCommandString)
End Sub

2) I launch the new processes (the ones I want to open in tabs in the browser window that I want to 'kill' later)

VBA Code:
Shell (sCommandString)
' sCommandString consists of the full path to the browser executable + a URL; it opens fine, and in the 'target' browser window

3) ' When I try to close it, I use this line:

VBA Code:
Call StopProcess(pHandle)

Sub StopProcess(TargetPHandle As Variant)
    Shell "TaskKill /F /PID " & TargetPHandle, vbHide
End Sub

but it doesn't appear to do anything.

Any ideas?
 
Upvote 0
Before you amended it and revised the code, di d you at least try it to see if it worked?
It seems to me that the key problem is that you've lost the handle. Can you please post the full code you plan to use so that I can see how and where the pHandle variable is declared.
 
Upvote 0
Before you amended it and revised the code, di d you at least try it to see if it worked?
It seems to me that the key problem is that you've lost the handle. Can you please post the full code you plan to use so that I can see how and where the pHandle variable is declared.
I apologize. Your code ran fine until I changed it. It opened Notepad then closed it.

Here is the code that I used. I am using Brave for my browser, and it would not close. I tried several other browsers and only "Vivaldi" closed when I asked it to.

VBA Code:
Public Sub cmdStart_Click()
.
.
.
Dim pHandle As Variant
OpenNotepad (pHandle)

VBA Code:
Sub OpenNotepad()
    
    'pHandle = Shell("Notepad.exe", vbNormalFocus)

    pHandle = Shell("C:\Program Files (x86)\BraveSoftware\Brave-Browser\Application\brave.exe", vbNormalFocus)
    Stop
    CloseProgram pHandle

    pHandle = Shell("C:\Program Files (x86)\Mozilla Firefox\firefox.exe", vbNormalFocus)
    Stop
    CloseProgram pHandle
    
    pHandle = Shell("C:\Program Files (x86)\Vivaldi\Application\vivaldi.exe", vbNormalFocus)
    Stop
    CloseProgram pHandle
    
    pHandle = Shell("C:\Program Files (x86)\Opera\launcher.exe", vbNormalFocus)
    Stop
    CloseProgram pHandle
    
    pHandle = Shell("C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", vbNormalFocus)
    Stop
    CloseProgram pHandle
    
End Sub

Sub CloseProgram(TargetPHandle As Variant)
    
    Shell "TaskKill /F /PID " & TargetPHandle, vbHide
    Debug.Print TargetPHandle & " killed"
    
End Sub
 
Upvote 0
That's ok - it's a bit odd that only one of the browsers would close though. When you look at the Immediate Window in the VBA Editor, does it have "[Some Number] killed" written 5 times?
Also, I would suggest making the pHandle variable a modular level variable. That means removing it from inside a subroutine (like you appear to have it currently), and putting it at the top of the module.
VBA Code:
Dim pHandle As Variant
Sub Browsers()
    
    pHandle = Shell("C:\Program Files (x86)\BraveSoftware\Brave-Browser\Application\brave.exe", vbNormalFocus)
    Debug.Print "Brave handle = " & pHandle
    Stop
    CloseProgram pHandle
    Stop

    pHandle = Shell("C:\Program Files (x86)\Mozilla Firefox\firefox.exe", vbNormalFocus)
    Debug.Print "Firefox handle = " & pHandle
    Stop
    CloseProgram pHandle
    Stop
    
    pHandle = Shell("C:\Program Files (x86)\Vivaldi\Application\vivaldi.exe", vbNormalFocus)
    Debug.Print "Vivaldi handle = " & pHandle
    Stop
    CloseProgram pHandle
    Stop
    
    pHandle = Shell("C:\Program Files (x86)\Opera\launcher.exe", vbNormalFocus)
    Debug.Print "Opera handle = " & pHandle
    Stop
    CloseProgram pHandle
    Stop

    pHandle = Shell("C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", vbNormalFocus)
    Debug.Print "Edge handle = " & pHandle
    Stop
    CloseProgram pHandle
    
End Sub

Sub CloseProgram(TargetPHandle As Variant)
    
    Shell "TaskKill /F /PID " & TargetPHandle, vbHide
    Debug.Print TargetPHandle & " killed"
   
End Sub
Make sure you give each program enough time to load and close down. For that reason, I put 'Stop' commands in between the browser opening and termination code.

The above revised code probably won't solve the problems for us, but this should give us a (slightly) better idea of what is going on.
 
Upvote 0
That's ok - it's a bit odd that only one of the browsers would close though. When you look at the Immediate Window in the VBA Editor, does it have "[Some Number] killed" written 5 times?
Also, I would suggest making the pHandle variable a modular level variable. That means removing it from inside a subroutine (like you appear to have it currently), and putting it at the top of the module.

I declared pHandle as a global in the header info of my frmMain, so I hope that's where you meant.

Yes, it is printing out the handles for each:

Brave handle = 25884
25884 killed
Firefox handle = 21920
21920 killed
Vivaldi handle = 10920
10920 killed
Opera handle = 24300
24300 killed
Edge handle = 28344
28344 killed
I put a timer for 30 seconds between the Shell commands to open the browser instances and attempting to close them via the TaskKill command. I hope that was enough time (it allowed for each browser to completely open). Same results; only Vivaldi was closed.
 
Upvote 0
Hi. Actually, no - the problem with variables declared in userforms is that they are, by default, private - meaning that they are not readily accessible outside of the userform. In the context of the above demo code, it doesn't much matter, because we're not ever 'leaving' the subroutine such that the handle reverts back to zero. For the above example, it's fine, but going forwards, it would be best to just put everything in its own standard module until I get a better feel for how your project is structured.

In any event, did it work? Did the browsers load? And then did they close down?
 
Upvote 0
I assume that it must've worked, otherwise it wouldn't have output the handle values (Edge handle = 28344, etc). Right?
 
Upvote 0
The browsers loaded but did not shut down (except Vivaldi... that one shut down when asked to for some yet unexplained reason.)
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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