BLACK THANOS
asked on
How do I unpin an application to the taskbar in windows 7 programatically.
Good evening experts,
On 10/19/2011 I posted the question:
How do I pin an application to the taskbar in windows 7 programatically.
On 10/19/2011 Run5k (Wizard) referred me to a wonderful code snippet from RobSampson (Genius) :
The solution pins items to the taskbar. I then started to research how to unpin those same items from the taskbar and found the following codr snippet from TsJurgen from Microsoft Answers:
I did not use that code I simply took the last part where it said "Unpin from Taskbar" and put it in RobSampsons code. Needless to say the results were less than desirable. I would like to used RobSampsons code to do my unpinning, as it is much simpler to understand. However, I would be grateful if someone were to instruct me on running the other code with whatever changes I need to make to unpin items from the windows 7 desktop. I ran the code from TsJurgen and got all kinds of errors. Lets just start from here and see what responses or questions you experts have in reference to my requirments for unpinning items from the windows 7 Desktop.
On 10/19/2011 I posted the question:
How do I pin an application to the taskbar in windows 7 programatically.
On 10/19/2011 Run5k (Wizard) referred me to a wonderful code snippet from RobSampson (Genius) :
arrFileNames = Array( _
"C:\Program Files\Microsoft Office\Office14\winword.exe" _
)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShellApp = CreateObject("Shell.Application")
Set objShell = CreateObject("WScript.Shell")
For Each strFilePath In arrFileNames
If objFSO.FileExists(strFilePath) = True Then
Set objFile = objFSO.GetFile(strFilePath)
Set objFolder = objShellApp.Namespace(objFile.ParentFolder & "\")
Set objFolderItem = objFolder.ParseName(objFile.Name)
Set colVerbs = objFolderItem.Verbs
For Each objVerb In colVerbs
If Replace(objVerb.name, "&", "") = "Pin to Start Menu" Then objVerb.DoIt
Next
WScript.Echo strFilePath & " has been pinned."
Else
WScript.Echo "Could not find " & strFilePath
End If
Next
If Replace(objVerb.name, "&", "") = "Pin to Start Menu" Then objVerb.DoIt
to this:
If Replace(objVerb.name, "&", "") = "Pin to Taskbar" Then objVerb.DoIt
The solution pins items to the taskbar. I then started to research how to unpin those same items from the taskbar and found the following codr snippet from TsJurgen from Microsoft Answers:
on error resume next
Const CSIDL_COMMON_PROGRAMS = &H17
Const CSIDL_PROGRAMS = &H2
Set oShell = CreateObject( "WScript.Shell" )
user=oShell.ExpandEnvironmentStrings("%UserName%")
comp=oShell.ExpandEnvironmentStrings("%APPDATA%")
WScript.Echo user & " " & comp
Set objShell = CreateObject("Shell.Application")
Set objAllUsersProgramsFolder = objShell.NameSpace(CSIDL_COMMON_PROGRAMS)
strAllUsersProgramsPath = objAllUsersProgramsFolder.Self.Path
msgbox comp & "Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar"
Set objFolder = objShell.Namespace(comp & "\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\")
Set objFolderItem = objFolder.ParseName("Windows PowerShell.lnk")
Set colVerbs = objFolderItem.Verbs
For Each objVerb in colVerbs
If Replace(objVerb.name, "&", "") = "Unpin from Taskbar" Then objVerb.DoIt
Next
Set objFolder = objShell.Namespace(comp & "\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\")
Set objFolderItem = objFolder.ParseName("Server Manager.lnk")
Set colVerbs = objFolderItem.Verbs
For Each objVerb in colVerbs
If Replace(objVerb.name, "&", "") = "Unpin from Taskbar" Then objVerb.DoIt
Next
I did not use that code I simply took the last part where it said "Unpin from Taskbar" and put it in RobSampsons code. Needless to say the results were less than desirable. I would like to used RobSampsons code to do my unpinning, as it is much simpler to understand. However, I would be grateful if someone were to instruct me on running the other code with whatever changes I need to make to unpin items from the windows 7 desktop. I ran the code from TsJurgen and got all kinds of errors. Lets just start from here and see what responses or questions you experts have in reference to my requirments for unpinning items from the windows 7 Desktop.
Rob, you're on top of things as usual! If you don't mind, I have a follow-up question. Envisioning the possibility of leaving a copy of the VBScript file containing all of your hard work in the Startup folder, how problematic would it be to eliminate the message that appears at the end stating that the item was pinned? I have tested your original code and it doesn't hurt to leave the VBS file in there... no duplicates or any other issues, but it might be nice to stop the redundant message from appearing every time that they login.
ASKER
Run5k,
I simply eliminated the messages and everything still works great. I dont need wscript.echo unless I am troubleshooting.
RobSampson,
I am testing your solution now.
I simply eliminated the messages and everything still works great. I dont need wscript.echo unless I am troubleshooting.
RobSampson,
I am testing your solution now.
ASKER
RobSampson:,
When I run your code I get the following error
Could not find a pinned item for C:\Program Files (x86)\Microsoft Office\Office14\winword.ex e
Exit code: 0 , 0000h
All my workstations or windows 7 64 bit and my office is still 32 bit , so I had to change:
"C:\Program Files\Microsoft Office\Office14\winword.ex e"
to
"C:\Program Files (x86)\Microsoft Office\Office14\winword.ex e"
When I run your code I get the following error
Could not find a pinned item for C:\Program Files (x86)\Microsoft Office\Office14\winword.ex
Exit code: 0 , 0000h
All my workstations or windows 7 64 bit and my office is still 32 bit , so I had to change:
"C:\Program Files\Microsoft Office\Office14\winword.ex
to
"C:\Program Files (x86)\Microsoft Office\Office14\winword.ex
ASKER
also, can this code be modified to unpin all applications from the taskbar without the benefit of know what is actually pinned?? Should I post another question referencing this ,because it is kind of outside of my original request?
Hi guys,
@Run5k, to eliminate any output messages, you can either comment out the WScript.Echo statements, and include On Error Resume Next at the top, or, if you're running it from a command, you can run
wscript //B c:\scripts\PinItems.vbs
to avoid any graphical output.
@Regis, here's a quick run down of how it goes about unpinning items.
From what I can tell, if you browse to this folder:
C:\Users\<username>\AppDat a\Roaming\ Microsoft\ Internet Explorer\Quick Launch\User Pinned\TaskBar
you will find shortcuts for each item on the taskbar.
The code will open each link (as long as it's a .lnk file) and test the TargetPath property against each file path that you specify in the array.
So, if you could find the .lnk for winword.exe in that folder, right-click it, and select properties, make sure the TargetPath matches the path that you put in the array.
If it's different, give me an example of how it differs, and I'll test it.
Regards,
Rob.
@Run5k, to eliminate any output messages, you can either comment out the WScript.Echo statements, and include On Error Resume Next at the top, or, if you're running it from a command, you can run
wscript //B c:\scripts\PinItems.vbs
to avoid any graphical output.
@Regis, here's a quick run down of how it goes about unpinning items.
From what I can tell, if you browse to this folder:
C:\Users\<username>\AppDat
you will find shortcuts for each item on the taskbar.
The code will open each link (as long as it's a .lnk file) and test the TargetPath property against each file path that you specify in the array.
So, if you could find the .lnk for winword.exe in that folder, right-click it, and select properties, make sure the TargetPath matches the path that you put in the array.
If it's different, give me an example of how it differs, and I'll test it.
Regards,
Rob.
Oh, you know what...I just remembered Office shortcuts are "advertised" shortcuts. I didn't test with one...I'll see what mine does now...
Rob.
Rob.
Hmmm....indeed, those shortcuts don't have a TargetPath....the file is called Microsoft Office Word 2007.lnk (or 2010 in your case).
I'll have to see what other properties we can do.....
The other option would be to specify the .lnk file name in the array, in which case we could unpin it easily....would be OK to do that?
Rob.
I'll have to see what other properties we can do.....
The other option would be to specify the .lnk file name in the array, in which case we could unpin it easily....would be OK to do that?
Rob.
OK, I can't find anything in the metadata of the office file that matches the .lnk file name. The next best thing is to specify the .lnk file name itself for unpinning.
Regards,
Rob.
Regards,
Rob.
' Example to pin items - specify the file path
arrFileNames = Array( _
"C:\Program Files\Microsoft Office 2007\Office12\WINWORD.EXE" _
)
' Example to unpin items - specify the shortcut file name from C:\Users\<username>\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar
arrFileNames = Array( _
"Microsoft Office Word 2007.lnk" _
)
'strMode can be "Pin" or "Unpin"
strMode = "unpin"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShellApp = CreateObject("Shell.Application")
Set objShell = CreateObject("WScript.Shell")
If LCase(strMode) <> "pin" And LCase(strMode) <> "unpin" Then
WScript.Echo "Mode is incorrect. Please set to ""pin"" or ""unpin""."
Else
strMode = LCase(strMode)
If strMode = "pin" Then
For Each strFilePath In arrFileNames
If objFSO.FileExists(strFilePath) = True Then
Set objFile = objFSO.GetFile(strFilePath)
Set objFolder = objShellApp.Namespace(objFile.ParentFolder & "\")
Set objFolderItem = objFolder.ParseName(objFile.Name)
Set colVerbs = objFolderItem.Verbs
For Each objVerb In colVerbs
'If Replace(objVerb.name, "&", "") = "Pin to Start Menu" Then objVerb.DoIt
If Replace(objVerb.name, "&", "") = "Pin to Taskbar" Then objVerb.DoIt
Next
WScript.Echo strFilePath & " has been pinned."
Else
WScript.Echo "Could not find " & strFilePath
End If
Next
ElseIf strMode = "unpin" Then
For Each strFilePath In arrFileNames
strTaskBar = objShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\"
blnFound = False
strFileName = ""
For Each objFile In objFSO.GetFolder(strTaskBar).Files
If LCase(objFile.Name) = LCase(strFilePath) Then
blnFound = True
Set objFolder = objShellApp.Namespace(strTaskBar)
Set objFolderItem = objFolder.ParseName(objFile.Name)
Set colVerbs = objFolderItem.Verbs
For Each objVerb In colVerbs
If Replace(objVerb.name, "&", "") = "Unpin from Taskbar" Or Replace(objVerb.name, "&", "") = "Unpin this program from Taskbar" Then objVerb.DoIt
Next
WScript.Echo strFilePath & " has been unpinned."
End If
Next
If blnFound = False Then WScript.Echo "Could not find a pinned item for " & strFilePath
Next
End If
End If
If you want to unpin all items, use "unpin_all" in the array, and this code will remove all of them.
Rob.
Rob.
' Example to pin items - specify the file path
arrFileNames = Array( _
"C:\Program Files\Microsoft Office 2007\Office12\WINWORD.EXE" _
)
' Example to unpin items - specify the shortcut file name from C:\Users\<username>\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar
' You can use "unpin_all" in the array to unpin all items.
arrFileNames = Array( _
"Microsoft Office Word 2007.lnk" _
)
'strMode can be "Pin" or "Unpin"
strMode = "unpin"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShellApp = CreateObject("Shell.Application")
Set objShell = CreateObject("WScript.Shell")
If LCase(strMode) <> "pin" And LCase(strMode) <> "unpin" Then
WScript.Echo "Mode is incorrect. Please set to ""pin"" or ""unpin""."
Else
strMode = LCase(strMode)
If strMode = "pin" Then
For Each strFilePath In arrFileNames
If objFSO.FileExists(strFilePath) = True Then
Set objFile = objFSO.GetFile(strFilePath)
Set objFolder = objShellApp.Namespace(objFile.ParentFolder & "\")
Set objFolderItem = objFolder.ParseName(objFile.Name)
Set colVerbs = objFolderItem.Verbs
For Each objVerb In colVerbs
'If Replace(objVerb.name, "&", "") = "Pin to Start Menu" Then objVerb.DoIt
If Replace(objVerb.name, "&", "") = "Pin to Taskbar" Then objVerb.DoIt
Next
WScript.Echo strFilePath & " has been pinned."
Else
WScript.Echo "Could not find " & strFilePath
End If
Next
ElseIf strMode = "unpin" Then
For Each strFilePath In arrFileNames
strTaskBar = objShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\"
blnFound = False
strFileName = ""
For Each objFile In objFSO.GetFolder(strTaskBar).Files
If LCase(objFile.Name) = LCase(strFilePath) Or LCase(strFilePath) = "unpin_all" Then
blnFound = True
Set objFolder = objShellApp.Namespace(strTaskBar)
Set objFolderItem = objFolder.ParseName(objFile.Name)
Set colVerbs = objFolderItem.Verbs
For Each objVerb In colVerbs
If Replace(objVerb.name, "&", "") = "Unpin from Taskbar" Or Replace(objVerb.name, "&", "") = "Unpin this program from Taskbar" Then objVerb.DoIt
Next
WScript.Echo strFilePath & " has been unpinned."
End If
Next
If blnFound = False Then WScript.Echo "Could not find a pinned item for " & strFilePath
Next
End If
End If
ASKER
RobSampson:,
You are a scripting God. Thank you for so much detail. I have just awoken so, I will be testing everything you just wrote and get back to you sometime today. Regardless, the points are yours, but I will make sure that I award them to one or more of the potential solutions you have posted
Regards.
RegisHyde
You are a scripting God. Thank you for so much detail. I have just awoken so, I will be testing everything you just wrote and get back to you sometime today. Regardless, the points are yours, but I will make sure that I award them to one or more of the potential solutions you have posted
Regards.
RegisHyde
ASKER
Hi Robsampson,
I tried your code above and I still get the dreaded Could not find a pinned item for C:\Program Files (x86)\Microsoft Office\Office14\winword.ex e
However, I used this code and it worked like a charm whereas before it did not:
I tried your code above and I still get the dreaded Could not find a pinned item for C:\Program Files (x86)\Microsoft Office\Office14\winword.ex
However, I used this code and it worked like a charm whereas before it did not:
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists("C:\Program Files (x86)") Then
Set objFolder = objFSO.GetFolder("C:\Program Files (x86)")
arrFileNames = Array( _
"C:\Program Files (x86)\Microsoft Office\Office14\winword.exe", _
"C:\Program Files (x86)\Microsoft Office\Office14\EXCEL.exe", _
"C:\Program Files (x86)\Microsoft Office\Office14\MSACCESS.exe", _
"C:\Program Files (x86)\Microsoft Office\Office14\OUTLOOK.exe", _
"C:\Program Files (x86)\Microsoft Office\Office14\POWERPNT.exe", _
"C:\Program Files (x86)\Microsoft Office\Office14\MSPUB.exe", _
"C:\Program Files (x86)\Microsoft Office\Office14\OIS.exe")
Else
arrFileNames = Array( _
"C:\Program Files\Microsoft Office\Office14\winword.exe", _
"C:\Program Files\Microsoft Office\Office14\EXCEL.exe", _
"C:\Program Files\Microsoft Office\Office14\MSACCESS.exe", _
"C:\Program Files\Microsoft Office\Office14\OUTLOOK.exe", _
"C:\Program Files\Microsoft Office\Office14\POWERPNT.exe", _
"C:\Program Files\Microsoft Office\Office14\MSPUB.exe", _
"C:\Program Files\Microsoft Office\Office14\OIS.exe")
End If
' Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShellApp = CreateObject("Shell.Application")
Set objShell = CreateObject("WScript.Shell")
For Each strFilePath In arrFileNames
If objFSO.FileExists(strFilePath) = True Then
Set objFile = objFSO.GetFile(strFilePath)
Set objFolder = objShellApp.Namespace(objFile.ParentFolder & "\")
Set objFolderItem = objFolder.ParseName(objFile.Name)
Set colVerbs = objFolderItem.Verbs
For Each objVerb In colVerbs
If Replace(objVerb.name, "&", "") = "Unpin from Taskbar" Then objVerb.DoIt
Next
' WScript.Echo strFilePath & " has been pinned."
Else
WScript.Echo "Could not find " & strFilePath
End If
Next
ASKER
What are your thoughts Robsampson??
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I hope you never leave the Experts Exchange community, because you are my goto guru, expert, genius, wizard.etc....
No worries Regis. Thanks for the kind words. It is much appreciated. I plan to stick around for a while. I enjoy the challenge!
Thanks for the grade.
Regards,
Rob.
Thanks for the grade.
Regards,
Rob.
I don't know if raising a dead post is the best route to take but when I try to use the script I get a WSH error box saying
line: 33
char: 5
error: Object required: 'objVerb'
code: 800A01A8
source: Microsoft VBScript runtime error
line: 33
char: 5
error: Object required: 'objVerb'
code: 800A01A8
source: Microsoft VBScript runtime error
Hi mostym, there was an error in that code, no doubt! Thanks for pointing that out. I obviously didn't test it enough!
I have now revised it, and updated the accepted comment. I have also added a "Both" option for strLocation, so you can perform the pin or unpin from both locations in the one run.
Regards,
Rob.
I have now revised it, and updated the accepted comment. I have also added a "Both" option for strLocation, so you can perform the pin or unpin from both locations in the one run.
Regards,
Rob.
Thanks Rob for fixing. Is there a way to first unpin everything, then pin?
Not yet, without running it twice. What I might be able to do is change the structure a bit so that you use something like:
so that it goes through those action specifications (three elements per action) and loops the process to do it.
I might be able to get that later this afternoon.
Regards,
Rob.
arrActions = Array( _
"unpin", "Both", "unpin_all", _
"pin", "Start Menu", "C:\Windows\Notepad.exe", _
"pin", "Taskbar", "C:\Windows\Calc.exe" _
)
so that it goes through those action specifications (three elements per action) and loops the process to do it.
I might be able to get that later this afternoon.
Regards,
Rob.
OK, this is untested at this point, since I have run out of time, but you can see how it goes.
Regards,
Rob,
Regards,
Rob,
' Specify the file paths of the file to pin or unpin
' If strMode = "unpin", you can use "unpin_all" in the arrFileNames array to unpin all items from the Taskbar or Start Menu.
' SYNTAX IS <mode>, <location>, <filename> WITH THREE ELEMENTS PER ACTION
arrActions = Array( _
"unpin", "Both", "unpin_all", _
"pin", "Start Menu", "C:\Windows\Notepad.exe", _
"pin", "Taskbar", "C:\Windows\Calc.exe" _
)
For intAction = 0 To (UBound(arrActions) - 2) Step 3
arrFileNames = Array(arrActions(intAction + 2))
'strMode can be "Pin" or "Unpin"
strMode = arrActions(intAction)
'strLocation can be "Start Menu" or "Taskbar" or "Both"
strLocation = arrActions(intAction + 1)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShellApp = CreateObject("Shell.Application")
Set objShell = CreateObject("WScript.Shell")
If LCase(strLocation) = "both" Then
arrLocations = Array("Start Menu", "Taskbar")
Else
arrLocations = Array(strLocation)
End If
For Each strLocation In arrLocations
If LCase(strMode) <> "pin" And LCase(strMode) <> "unpin" Then
WScript.Echo "Mode is incorrect. Please set to ""pin"" or ""unpin""."
WScript.Quit
ElseIf LCase(strLocation) <> "start menu" And LCase(strLocation) <> "taskbar" Then
WScript.Echo "Location is incorrect. Please set to ""Start Menu"" or ""Taskbar""."
WScript.Quit
Else
strMode = LCase(strMode)
If strMode = "pin" Then
strVerb = LCase(strMode & " to " & strLocation)
strMessage = " has been " & strMode & "ned to the " & strLocation & "."
ElseIf strMode = "unpin" Then
strVerb = LCase(strMode & " from " & strLocation)
strMessage = " has been " & strMode & "ned from the " & strLocation & "."
End If
For Each strFilePath In arrFileNames
If LCase(strFilePath) = "unpin_all" And strMode = "unpin" Then
strPinLocation = objShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Internet Explorer\Quick Launch\User Pinned\" & Replace(strLocation, " ", "") & "\"
For Each objFile In objFSO.GetFolder(strPinLocation).Files
strFullPath = objFile.Path
'Set objFile = objFSO.GetFile(objFile.Path)
Set objFolder = objShellApp.Namespace(objFile.ParentFolder & "\")
Set objFolderItem = objFolder.ParseName(objFile.Name)
Set colVerbs = objFolderItem.Verbs
For Each objVerb In colVerbs
If LCase(Replace(objVerb.name, "&", "")) = strVerb Then
objVerb.DoIt
WScript.Echo strFullPath & strMessage
End If
Next
Next
Else
If objFSO.FileExists(strFilePath) = True Then
Set objFile = objFSO.GetFile(strFilePath)
Set objFolder = objShellApp.Namespace(objFile.ParentFolder & "\")
Set objFolderItem = objFolder.ParseName(objFile.Name)
Set colVerbs = objFolderItem.Verbs
blnOptionFound = False
For Each objVerb In colVerbs
If LCase(Replace(objVerb.name, "&", "")) = strVerb Then
objVerb.DoIt
blnOptionFound = True
End If
Next
If blnOptionFound = True Then
WScript.Echo strFilePath & strMessage
Else
WScript.Echo "Unable to " & strMode & " " & strFilePath & " from the " & strLocation & ". The verb does not exist."
End If
Else
WScript.Echo "Could not find " & strFilePath
End If
End If
Next
End If
Next
Next
I have pieced them together, and added strMode for you to specify whether to "Pin" or "Unpin" items.
Regards,
Rob.
Open in new window