How to enable / disable screen saver in Windows from VB

Method
To enable or disable the screen saver, use the SystemParametersInfo Windows API.

Here is an example on how to use it from VB. For more information, search for Microsoft article Q126627 in the MSDN help. You can also use the VB API Viewer to copy and paste the declarations used in this example.

Code Example
Option Explicit

Private Declare Function SystemParametersInfo Lib "user32" _
Alias "SystemParametersInfoA" _
(ByVal uAction As Long, ByVal uParam As Long, _
ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long

Const SPI_GETSCREENSAVEACTIVE=16
Const SPI_SETSCREENSAVEACTIVE=17
Const SPIF_SENDWININICHANGE=&H2

Public Sub EnableScreenSaver(enable As Boolean)

SystemParametersInfo SPI_SETSCREENSAVEACTIVE, _
enable, 0&, SPIF_SENDWININICHANGE

End Sub

Public Function IsScreenSaverActive() As Boolean

Dim pvParam As Long
SystemParametersInfo SPI_GETSCREENSAVEACTIVE, 0, pvParam, 0
IsScreenSaverActive=(pvParam=1)

End Function

To enable the screen saver, call EnableScreenSaver with an argument of True.

EnableScreenSaver True

To disable the screen saver, call EnableScreenSaver with an argument of False.

EnableScreenSaver False