The following code selects arg1 in a Windows Explorer folder:

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("""" & WScript.Arguments(0) & """")
WScript.Sleep 400

' objShell.SendKeys("^a{F5}")       ' Deslect All.

Set objShellAPP = CreateObject("Shell.Application")
On Error Resume Next        ' For new unsaved files.
With objShellAPP.Windows(objShellAPP.Windows.Count - 1).document
.SelectItem .Folder.Items.Item(WScript.Arguments(1)), 17
End With

Set objShell = Nothing
Set objShellAPP = Nothing

If fileA is already selected in the folder and fileB is passed as arg1, both files are selected.

How can I have fileA deselected and only fileB selected? The commented line objShell.SendKeys("^a{F5}")is a workaround, but there must be a better way.

Thank you.

1 Answer 1


You pass the value of 17 for the parameter dwFlags. This value is an integer representing the combination of flags you want set.

You derive this value by a bitwise OR operation using the flag values. I think 17 is probably 16 OR 1 (give the item focus; select item) There is a flag value of 4 to "deselect all but the specified item." 17 OR 4 = 21.

Use OR to combine flags; use AND to test against a particular flag to see if it is set (21 AND 4 = 4); use AND not to toggle one flag off (21 AND not(4) = 17)

SelectItem Documentation

random link: Stackoverflow - How do integer flags work?

  • Note: flags make more sense if you consider them in binary and then consider that binary is analogous with a bank of toggle switches. So "8" is 1000 or On-Off-Off-Off.
    – Yorik
    Jul 18, 2017 at 21:55
  • Perfect. Thank you very much. 21 works like a charm.
    – Yaron
    Jul 18, 2017 at 23:09
  • superuser.com/questions/1233283/…
    – Yaron
    Jul 22, 2017 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.