Excel/VBA MVP's: Restart Windows Service with a Macro?

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622
Is it possible to restart a Windows Service with execution from a VBA Macro?

When you go into Windows Computer Management you can see a list of services. You can click a hyperlink to start, pause or restart.





Thanks,
 
Those Private Declares need to go at the top of your module (after any Option statements) but before any other Subs or Functions.
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Ok I fixed that

To make sure I understand the new code.... I need to put in the service name to alter. Then if I wanted to stop the service, I would comment out all debug.print lines except the last one and run Example() ?
 
Last edited:
Upvote 0
For the most part, this is probably good enough...

Code:
    If Service("YourService", "stop") Then
        'ok
    Else
        'problem
    End If
 
Upvote 0
Hrm,

The conditions is true, but the service remains running. Something strange happened earlier too. All of these strange windows filled the task bar- the windows looked like windows services....

Edit: Does it matter if there are spaces in the service name? Do I need to use %20 or something instead?
 
Last edited:
Upvote 0
Are you using the name (Display Name) or the service NAME. :) The listing you see under the name header in services is not the name we are after. Double click on the service to bring up the properties dialog. See the name we need in there. As for strange windows and other strange happenings? That is normal when you run any code I have written. :)
 
Upvote 0
Good call. I was using, "Network DDE DSDM" and it is actually, "NetDDEdsdm" - I'll try that and let you know.
 
Upvote 0
start = 2
stop = continues to run?
pause = 0
continue = 0

I think the pause is the only thing working...when I click continue, I no longer see updates. Unless if the workbook must be closed and re-opened each time...

also can I do this:
Code:
    If Service("NetDDEdsdm", "stop") = 0 Then
        'ok
        stp = MsgBox("Attempted to stop Network DDE DSDM service", vbOKOnly, "Stop Service")
        Exit Sub
   ElseIf Service("NetDDEdsdm", "stop") = 1 Then
        stp = MsgBox("Could not stop Network DDE DSDM service", vbOKOnly, "Stop Service")
        Exit Sub
    Else
        'problem
        stp2 = MsgBox("Already in progress or not supported.", vbOKOnly, "Stop Service")
        Exit Sub
    End If
 
Last edited:
Upvote 0
Yes,

Pause works (I see that after I refresh)
Stop does not...I have to stop the code like it's an infinite loop
Continue works (after paused)

Ok, stopping this service will stop another, so I'm getting a prompt when I stop from the computer management window.

So do I stop the influenced service, then stop the service i'm after? Probably the same for starting as well?
 
Upvote 0
If there are dependancies? I don't know how to deal with that. I have only had to use this to stop services that I have created myself. They were not part of any tree.
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,047
Members
449,206
Latest member
Healthydogs

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