Is it possible to Close / Stop internet connection in VBA

chrisbrocco

Board Regular
Joined
Mar 31, 2005
Messages
82
I have a broad band connection and would like to be able to close my internet connection from excel.

I currently use a series of Send keys, but it does not always work.

Any suggestions how this could be done
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Andrew Fergus

MrExcel MVP
Joined
Sep 9, 2004
Messages
5,432
Hi Chris

The following code will stop a process, provided you know the process name (usually the programme name followed by .exe) :

Code:
Public Function StopMyProcess(strTerminateThis As String)

On Error Resume Next

Dim objWMIcimv2 As Object
Dim objProcess As Object
Dim objList As Object
Dim intError As Integer
     
Set objWMIcimv2 = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\.\root\cimv2")
     
Set objList = objWMIcimv2.ExecQuery _
    ("select * from win32_process where name='" & strTerminateThis & "'")

If objList.Count = 0 Then
    'The process isn't running
    MsgBox "The process isn't running."
Else
    'The process is running
    For Each objProcess In objList
        'Terminate the process and all of its threads.
        intError = objProcess.Terminate
        If intError <> 0 Then
            'Return value is 0 for success. Any other number is an error.
            MsgBox "There was an error closing this process."
        Else
            MsgBox "Process terminated ok."
        End If
    Next
    Set objProcess = Nothing
End If

Set objWMIcimv2 = Nothing
Set objList = Nothing

End Function

Call this code by passing the process name into the function like so:

StopMyProcess("firefox.exe") {for example}

HTH, Andrew
 

chrisbrocco

Board Regular
Joined
Mar 31, 2005
Messages
82
This is a really neat piece of code, which I can definetly use further in my code, but all though it would close all sessions of firefox for example my broadband connection is still connected.

would you know how to actually terminate my broadband connection

many thanks
 

Andrew Fergus

MrExcel MVP
Joined
Sep 9, 2004
Messages
5,432
Hi Chris

I only used firefox as an example. Change the firefox part to the name of your DSL programme that you want to close. I can't tell you which process to close because I don't know how you connect to the internet - there are many many ways of doing that. If you open your task manager and can identify which is the DSL connection process, then that is the name you want to pass into the function.

Also remove any of the message boxes that you don't want to see, they were only included for de-bugging / feedback purposes.

Andrew
 

actorcat

New Member
Joined
Apr 12, 2018
Messages
1
i put in : StopMyProcess("firefox.exe") and it says Expected:identifier, i want it to close firefox after 30 min or an hour or so... but cant make it work
 

Forum statistics

Threads
1,136,267
Messages
5,674,730
Members
419,523
Latest member
Urnovio

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
Top