Changing and updating the desktop wallpaper - only in vbscript

Hey all. I've got a vbscript that I wrote that will make the necessary change to the registry to set an HTML file as the desktop wallpaper.

The only problem I'm having is actually getting the desktop to refresh so that the new wallpaper is displayed. The code I have does manage to send an F5 keystroke to refresh the screen, but the wallpaper doesn't show up. When I access the display properties, I can see that the wallpaper is selected, so how do you get it to show up once you've set the registry value?

**********************************************
BEGIN CODE
**********************************************
' Create the variables needed
Dim WSHShell
Dim strRegKey
Dim strWallpaper
Dim strDesktop

' Create the Wscript Shell object
Set WSHShell = WScript.CreateObject("WScript.Shell")

' Define the wallpaper
strWallpaper = "%SystemRoot%\Web\Wallpaper\PRWeb.html"
strDesktop    = WSHShell.SpecialFolders("Desktop")

' Create the wallpaper key
strRegKey =  "HKEY_CURRENT_USER\"
strRegKey = strRegKey & "Software\"
strRegKey = strRegKey & "Microsoft\"
strRegKey = strRegKey & "Internet Explorer\"
strRegKey = strRegKey & "Desktop\"
strRegKey = strRegKey & "General\"
strRegKey = strRegKey & "Wallpaper"

' Write the key, type, and value
WSHShell.RegWrite strRegKey, strWallpaper, "REG_EXPAND_SZ"

' Refresh the desktop now.
WSHShell.AppActivate strDesktop
WSHShell.SendKeys "{F5}"

' Quit
Wscript.quit(0)
**********************************************
END CODE
**********************************************
NebulusAsked:
Who is Participating?
 
bruintjeConnect With a Mentor Commented:
In addition to the logon/logoff you can consider this options also to refresh the desktop:

1. The correct (but "complicated") way of doing it:
Use the API SystemParametersInfo through an API wrapper or a COM object.

2. The "dirty" but simple way:
Use the Control panel applet desk.cpl and SendKeys to refresh the desktop.

Set oSHApp = CreateObject("Shell.Application")
Set oShell = CreateObject("WScript.Shell")

' set the desired wallpaper in registry ("HKCU\Control
Panel\Desktop\Wallpaper") using eg. RegWrite

oSHApp.ControlPanelItem cstr("desk.cpl")
' alt. oShell.Run "control desk.cpl"

Do Until oShell.AppActivate ("Display Properties")
Loop
oShell.SendKeys "{down}{up}{tab 3}a~"

If the {down}{up}- thing doesn't work for you, you must do something else in the control panel applet to activate a change (but that really not changes anything ;-) to "activate" the apply-button. NB! Key sequence is very often dependent on the language version of Windows.

source : http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=3B7199E3.C72BC69F%40hydro.com
0
 
NebulusAuthor Commented:
That worked perfectly! I made some *minor*changes, and here's the final code. Thanks for the help!

' Create the variables needed
Dim WSHShell
Dim WSHApp
Dim strRegKey
Dim strWallpaper
Dim strOSRoot
Dim strDesktop
Dim strDisplayProp

' Create the Wscript Shell object
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set WSHApp   = CreateObject("Shell.Application")

' Define the wallpaper
strWallpaper   = "%SystemRoot%\Web\Wallpaper\PRWeb.html"
strDesktop     = WSHShell.SpecialFolders("Desktop")
strDisplayProp = "Display Properties"

' Create the wallpaper key
strRegKey =  "HKEY_CURRENT_USER\"
strRegKey = strRegKey & "Software\"
strRegKey = strRegKey & "Microsoft\"
strRegKey = strRegKey & "Internet Explorer\"
strRegKey = strRegKey & "Desktop\"
strRegKey = strRegKey & "General\"
strRegKey = strRegKey & "Wallpaper"

' Write the key, type, and value
WSHShell.RegWrite strRegKey, strWallpaper, "REG_EXPAND_SZ"

' Refresh the desktop now.
WSHApp.ControlPanelItem cstr("desk.cpl")
Do Until WSHShell.AppActivate (strDisplayProp)
Loop
WSHShell.SendKeys "{down}{up}{tab 3}a~"
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.