Wednesday, December 22, 2010

VBScript equivalent to F5 key

I spend a considerable amount of time trying to come up with VBScript equivalent of F5 key for refreshing the desktop and as it turn out, SendKey "{f5}" method alone does not do the job. Below is the script, utilizing Windows Explorer shell command file (.scf)
that worked like a charm:


' Create explorer command file to toggle desktop window
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFile= objFSO.BuildPath(objFSO.GetSpecialFolder(2), objFSO.GetTempName &".scf")
With objFSO.CreateTextFile(strFile, True)
.WriteLine("[Shell]")
.WriteLine("Command=2")
.WriteLine("[Taskbar]")
.WriteLine("Command=ToggleDesktop")
.Close
End With

' Toggle desktop and send F5 to refresh desktop
With CreateObject("WScript.Shell")
.Run """" & strFile & """"
WScript.Sleep 100
.Sendkeys "{F5}"
End With
' Delete command file
objFSO.DeleteFile strFile

2 comments:

  1. newbie here. How to refresh desktop without minimizing other windows to the taskbar?

    ReplyDelete
  2. do they get minimized after you execute the code above?

    ReplyDelete