How to kill chrome with VBA

DCFirecracker

New Member
Joined
Aug 24, 2019
Messages
1
Hi. I need to make sure that Chrome is not running in the background during certain Excel events. I found the following code on a different forum from a thread posted a few years ago. It works (i.e., it calls the macro when the event occurs and closes Chrome) but it also throws a debugging error (i.e., runtime error: not found) related to the objProcess.Terminate line of code. I'm currently running Excel 2016 and I'm pretty new to VBA coding. Does anyone have any suggestions on how to resolve the error?

Sub FindAndTerminate(ByVal strProcName As String)
Dim objWMIService, objProcess, colProcess
Dim strComputer
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!" _
& strComputer & "\root\cimv2")
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & strProcName & "'")
If colProcess.Count > 0 Then
For Each objProcess In colProcess
objProcess.Terminate
Next objProcess
End If
End Sub


'Call with:'
'FindAndTerminate "chrome.exe" as part of a worksheet event sub'
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Hi. I need to make sure that Chrome is not running in the background during certain Excel events. I found the following code on a different forum from a thread posted a few years ago. It works (i.e., it calls the macro when the event occurs and closes Chrome) but it also throws a debugging error (i.e., runtime error: not found) related to the objProcess.Terminate line of code. I'm currently running Excel 2016 and I'm pretty new to VBA coding. Does anyone have any suggestions on how to resolve the error?
Try this.
Code:
On error resume next

CreateObject("wscript.shell").Run "cmd /c """ & "taskkill /IM chrome.exe >nul""""",0,true

https://superuser.com/questions/727724/close-programs-from-the-command-line-windows
 
Upvote 0
Hello DCFirecracker,

Is the error number you received 80041002 ?
Which Windows OS are you using ?

I did find an error in your code. This code works on my computer Windows 7 with Office 2010.
Code:
Sub FindAndTerminate(ByVal strProcName As String)

    Dim objWMIService   As Object
    Dim objProcess      As Object
    Dim colProcess      As Object
    Dim strComputer     As String

        strComputer = "."
    
        Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        Set colProcess = objWMIService.ExecQuery("Select * from Win32_Process Where Name = '" & strProcName & "'")

        For Each objProcess In colProcess
            objProcess.Terminate
        Next objProcess

End Sub
 
Last edited:
Upvote 0
Hello. I have a vba subroutine that opens a web browser so the user can download a file

chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Shell (chromePath & " -url CNN - Breaking News, Latest News and Videos")
I am calling this in a subroutine called by a button on a form. Once the users downloads the files from the Url I would like to close the instance of chrome and continue through the macro leaving the form that calls the above code.

the actual url used in my code is not cnn used it just for demo purpose

I have tried the send key method to send key 'SendKeys "%{F4}", True' to close window, but it just closes the loaded form. Is there a way to put focus on the chrome window so that f SendKeys "%{F4}", True works?? or another method that would close it without exiting my running vba code. thanks for your help.
 
Upvote 0
Hello. I have a vba subroutine that opens a web browser so the user can download a file

chromePath = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
Shell (chromePath & " -url CNN - Breaking News, Latest News and Videos")
I am calling this in a subroutine called by a button on a form. Once the users downloads the files from the Url I would like to close the instance of chrome and continue through the macro leaving the form that calls the above code.

the actual url used in my code is not cnn used it just for demo purpose

I have tried the send key method to send key 'SendKeys "%{F4}", True' to close window, but it just closes the loaded form. Is there a way to put focus on the chrome window so that f SendKeys "%{F4}", True works?? or another method that would close it without exiting my running vba code. thanks for your help.
Well first of all consider your approach to doing this. Do you have direct links to the files you want downloaded? If so, you can download it for them in the background. Otherwise use the code I posted previously in this thread to close chrome. I'm not sure if it will work on MAC but it does on Windows.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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