2015-08-20 49 views
1

我在VBA上使用ShellExecute強制計算機進入休眠狀態(不休眠),因此我需要禁用休眠模式。我輸入了rundll32.exe,但在「powercfg.cpl -hibernate off」中出現錯誤。VBA - 如何使用ShellExecute強制計算機進入休眠狀態(而不是休眠)

我也試過:

powercfg.exe /hibernate off 

powercfg -hibernate off 
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.cpl -h off", "C:\", SW_SHOWNORMAL 
    ShellExecute 0, "runas", "C:\WINDOWS\System32\rundll32.exe", "powrprof.dll,SetSuspendState 0,1,0", "C:\", SW_SHOWNORMAL 
    ShellExecute 0, "runas", "C:\WINDOWS\System32\rundll32.exe", "powercfg.cpl -hibernate on", "C:\", SW_SHOWNORMAL 
End Sub 

回答

0

後相當多的試驗和錯誤我找到了解決辦法。這將使您的計算機通過VBA進入睡眠模式:

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", "powercfg.exe", "/hibernate off", "C:\", SW_SHOWNORMAL 
    Shell "C:\WINDOWS\System32\rundll32.exe powrprof.dll,SetSuspendState 0,1,0" 
    ShellExecute 0, "runas", "powercfg.exe", "/hibernate on", "C:\", SW_SHOWNORMAL 
End Sub