Need help with vbscript to create shortcut on desktop-refuses to run cause of correct syntax error.

stephenlecomptejr
stephenlecomptejr used Ask the Experts™
on
I'm trying to create a shortcut automatically using vbs script but it refuses to run all the way through  because it throws a char: 2 error - object doesn't support this property or method which happens on this particular line item:

WshShell = CreateObject("Wscript.shell")

Please note attachment that shows error.

I took this code from this website but it hasn't worked out for me.

https://support.microsoft.com/en-us/help/244677/how-to-create-a-desktop-shortcut-with-the-windows-script-host

Public Sub CreateIconToDesktop()

 Dim WshShell
 Dim strDesktop
 Dim oMyShortCut
 WshShell = CreateObject("Wscript.shell")
 strDesktop = WshShell.SpecialFolders("Desktop")
 oMyShortcut = WshShell.CreateShortcut(strDesktop + "\Sample.lnk")
 'oMyShortcut.WindowStyle = 3  &&Maximized 7=Minimized  4=Normal 
 oMyShortcut.IconLocation = "Q:\Dialer\_dbadmin\icons\logocampaign.ico"
 oMyShortcut.TargetPath = "Q:\Dialer\dialercampaigns.accdb"
 'oMyShortCut.Hotkey = "ALT+CTRL+F"
 oMyShortCut.Save

End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Are you running this as a VBS script, or as VBA code inside an Office app?

One thing I do see wrong if a stand alone VBS, you need a Set when assigning object variables, like:

Set WshShell = CreateObject("Wscript.shell")


»bp
IT / Software Engineering Consultant
Top Expert 2016
Commented:
You were missing another SET.  This seems to work here, as a standalone VBS script.

Call CreateIconToDesktop()

Public Sub CreateIconToDesktop()

 Dim WshShell
 Dim strDesktop
 Dim oMyShortCut
 Set WshShell = CreateObject("Wscript.shell")
 strDesktop = WshShell.SpecialFolders("Desktop")
 Set oMyShortcut = WshShell.CreateShortcut(strDesktop + "\Sample.lnk")
 'oMyShortcut.WindowStyle = 3  &&Maximized 7=Minimized  4=Normal 
 oMyShortcut.IconLocation = "Q:\Dialer\_dbadmin\icons\logocampaign.ico"
 oMyShortcut.TargetPath = "Q:\Dialer\dialercampaigns.accdb"
 'oMyShortCut.Hotkey = "ALT+CTRL+F"
 oMyShortCut.Save

End Sub

Open in new window


»bp

Author

Commented:
Dude you the man!  Thank you so much.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Welcome, glad that helped.

~bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial