0
votes

I'm trying to get the computer to sleep through excel VBA. For that to happen I need to disable hybernation which can only be done with elevated permissions. So I'm trying ShellExecute - following Microsoft's instructions on this page, but I'm getting an runtime error ('49' Bad DLL calling convention).

Edit: Changed the code and it works now, but the parameters are incorrect.

Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
                                                             ByVal hwnd As Long, _
                                                             ByVal lpszOp As String, _
                                                             ByVal lpszFile As String, _
                                                             ByVal lpszParams As String, _
                                                             ByVal lpszDir As String, _
                                                             ByVal FsShowCmd As Long) as long
Const SW_SHOWNORMAL as long = 1
Sub DoSleep()
    ShellExecute 0, "runas", "C:\WINDOWS\System32\rundll32.exe", "powercfg -h off", "C:\", SW_SHOWNORMAL
    Shell "C:\WINDOWS\System32\rundll32.exe powrprof.dll,SetSuspendState 0,1,0"
    ShellExecute 0, "runas", "C:\WINDOWS\System32\rundll32.exe", "powercfg -h on", "C:\", SW_SHOWNORMAL
End Sub
1
Did you define SW_SHOWNORMAL?Bond
Add As Long to the end of the declaration. You're returning a Variant because you haven't specified the return type so it doesn't match the function declaration in the DLL.Bond
Added as long and it now runs without the error.jony

1 Answers

2
votes

There is a problem with the definition of SW_SHOWNORMAL. The way you have defined it assumes it is an Integer (default for VBA which is a 16 bit value) where it needs to be Long (32 bit value).

Change your declaration to this:-

Const SW_SHOWNORMAL As Long = 1