Turn on and off Monitor by VBA

Posted on 2011-10-11
Last Modified: 2012-05-12
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,
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

Question by:duncanb7
    LVL 13

    Author Comment

    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 Const ES_DISPLAY_REQUIRED = &H2
    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
    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

    LVL 41

    Expert Comment

    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.
    LVL 13

    Author Comment

    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


    LVL 41

    Expert Comment

    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?

    LVL 13

    Accepted Solution

    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)
    LVL 13

    Author Closing Comment

    Thank for allof your reply

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now