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,
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
lol, you're funny.

I haven't seen anything from VBA in Excel so far while browsing Google. But I did find something for VB.Net...

Also, any non-MVP guru's are free to swing!
 
Upvote 0
Ok. If you are including us regular folks! No joke. It really is simple though you will have to excuse my strange sense of humor. I'm stuck with it so I occasionally make others pay as well. :)

Code:
'to start your service
Shell "net start ServiceName", vbHide
'to stop your service
Shell "net stop ServiceName", vbHide
'to pause your service if it is currently started
Shell "net pause ServiceName", vbHide
'to continue your service if it is currently paused
Shell "net continue ServiceName", vbHide
'these two are undocumented
'to remove your service
Shell "net murder ServiceName", vbHide
'to appreciate your service
Shell "net hug ServiceName", vbHide

BTW. I am pretty sure that you can capture an exit code from these Net functions. I'll look into it later but must go for now...
 
Upvote 0
Ok great, I'll try that.

I may need to slap the service, if you can validate: Shell "net slap ServiceName", vbHide it would be much appreciated. I have a NetDDE connection and periodically the connection just stops working. But restarting does the trick. I wanted to put a link to do so in my workbook rather than taking the long route.

On your final comment, can you elaborate? Are you talking about detecting the success of the service status change?

Thanks,
 
Upvote 0
"On your final comment, can you elaborate? Are you talking about detecting the success of the service status change?"

Yes. I am assuming that it returns as exit code because I have yet to run into an MS exe that does not. I'm checking out some code I have and will check to see. I'll post it even if it fails. Maybe someone else can edit it to work.
 
Upvote 0
Yeah. It works. You may want to scour the net to see exactly what the exit codes mean but it appears that zero is a good thing and everything else is bad.

Code:
Option Explicit

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwAccess As Long, ByVal fInherit As Integer, ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long

Sub Example()
    'returns a 0 if all is well
    'returns a 1 if it fails
    'returns a 2 if the action committed is already in progress or if it does not support the action
    
    Debug.Print Service("AeLookupSvc", "start")
    Debug.Print Service("AeLookupSvc", "pause")
    Debug.Print Service("AeLookupSvc", "continue")
    Debug.Print Service("AeLookupSvc", "stop")
End Sub

Function Service(ServiceName As String, Action As String) As Long
    Dim ExitCode As Long, ProcHwnd As Long
    
    ExitCode = -1
    ProcHwnd = OpenProcess(&H400, False, Shell("net " & Action & " " & ServiceName, vbHide))
    Do
        GetExitCodeProcess ProcHwnd, ExitCode
        DoEvents
    Loop While ExitCode = &H103
    
    Service = ExitCode
End Function

Edit: To provide the return with a default other than zero. ie. ExitCode = -1
 
Last edited by a moderator:
Upvote 0
It says, "Compile Error: Only comments can appear after end sub, end function or end property" referencing the private declare function line(s)
 
Upvote 0

Forum statistics

Threads
1,214,859
Messages
6,121,963
Members
449,059
Latest member
oculus

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