Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1564
  • Last Modified:

Turn on and off Monitor by VBA

Dear Expert,

I would like to program code in EXcel2003 VBA to turn on or off my monitor in my window XP
computer  and I tried the code in the link, http://www.expertsexchange.com/Programming/Misc/Q_20840222.html
and set lparm in sendmessage API function from 1 or 2, But it doesn't have any response on
my monitor, Why ? lparm=2 is setting to turn off and 1 is setting for turn-on

Is it hwnd handle that can be any window's handle in attached VBA code ? or other setting I need
to set for hwnd input in oder to  turn on or off monitor ?
ANd it doesn't work  , is it because my montherboard not supporting the function ?
Please advise

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
 Private Const WM_SYSCOMMAND = &H112
 Private Const SC_MONITORPOWER = &HF170
Sub Monitor()
Dim hwnd As Long
hwnd = GetForegroundWindow()
Call SendMessage(hwnd, WM_SYSCOMMAND, SC_MONITORPOWER, 2)  
End Sub

Open in new window

  • 4
  • 2
1 Solution
duncanb7Author Commented:
The problem is solved if using SetThreadExecutionState(ES_DISPLAY_REQUIRED)
and monitor is on once computer is startup from sleep mode, and
logon welcome page can be disabled by  control\user account and uncheck  welcome page option
but it is still existing Adminstrator logn box even welcome page is gone.
So the final issue is Adminstrator logon box can not be disabled , Why ?

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esFlags As Long) As Long

Private Sub workbook_open()
'Sleep 80000
Dim rt As Long
rt = SetThreadExecutionState(ES_DISPLAY_REQUIRED)
'for delete welcom login page on xp, just tye at command line, "control userpasswords2"
' for delete adminstrator login message box , read at http://www.ehow.com/how_5959387_stop-logon-startup-win-xp.html
Set WsShell = CreateObject("WScript.Shell")
'Sleep (1000)
'WsShell.Run ("TaskMgr.exe")

'Sleep (5000)
'WsShell.SendKeys ("sea")

'WsShell.SendKeys ("{enter}")
'this.Caption = Hex(rt)
End Sub

You can disable Administrator logon with the command prompt:

Net User Administrator /active:no

You should not get an Administrator login option after that - but, Administrator will be disallowed login as well.
duncanb7Author Commented:
What you mean if I type  command  at C:> Net User Administrator /active:no
but, Administrator will be disallowed login as well ?

I am only the user on my computer  with window xp, and you
mean "but, Administrator will be disallowed login as well"
 that I can not login my comptuer if type Net User Administrator /active:no
at command line.

Just want to auto login to my desktop screen without going through any login box
Please advise


Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

You had asked why the Administrator cannot be disabled.  Perhaps I misunderstood.  I just gave you the command to deactivate Admin login.

Your question is how to autologin as Administrator?

duncanb7Author Commented:
please don't use Net User Administrator /active:no if it is only one account  in xp such as only Administrator
and otherwise you will be locked and could not login into computer again besides you need
to reboot the comptuer into safe mode.

THe final solution is

1- type command  at command line or run with "control userpassword2s , check the box the
password require during login and then select your account in the account name menu right below
the box that action is for putting your account in first priority or as deafult login name and password
for auto login process , and then uncheck the password require for login box in order to
enable auto login process with your default your login name and password, so next time
you will be automatically logging into Xp without typing your login and password from hiberation mode

2-use rt = SetThreadExecutionState(ES_DISPLAY_REQUIRED) in the VBA code and put the code
into xls's workbook_open() sub in Thisworkbook module and set  the xls file  as one schedule task to wake up the computer from sleep or hiberation mode and the monitor will be wake up  by the setThreadExecutionState()
3-go to  start menu/control-panel/power management option/advance tab/ and  uncheck the box for password require when computer is wkae up from hiberation mode

Now I can wake my xp computer and also wake up monitor for any other schedule task or program  work which must need monitor is on. (Of course most of schedule task program or update is no need monitor on)
duncanb7Author Commented:
Thank for allof your reply

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now