VB Script to create URL link not working

Answered VB Script to create URL link not working

  • Wednesday, May 02, 2012 2:47 PM
     
      Has Code

    Referring to the following reference:

    http://support.microsoft.com/kb/244677

    I have created a .vbs file as in the third example:

    WshShell = CreateObject("WScript.Shell")
    strDesktop = WshShell.SpecialFolders("Desktop")
    oUrlLink = WshShell.CreateShortcut(strDesktop+"\Microsoft Web Site.URL")
    oUrlLink.TargetPath = "http://www.microsoft.com"
    oUrlLink.Save

    ...and I get this error:

    C:\Scripts\url.vbs(1, 1) Microsoft VBScript runtime error: Object doesn't support this property or method: 'WshShell'

    Anyone?


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

All Replies

  • Wednesday, May 02, 2012 2:54 PM
     
     Answered Has Code

    It's missing a "Set" on the creation of the shortcut object.

    Set objShell = WScript.CreateObject("WScript.Shell")
    strDesktop = objShell.SpecialFolders("AllUsersDesktop")
    Set objMyShortcut = objShell.CreateShortcut(strDesktop & "\MYLINK.lnk")
    objMyShortcut.TargetPath = "http://www.microsoft.com"
    objMyShortCut.Save


    Edit:  Well and on the shell object as well.
    Edit2:  Interestingly enough, that Microsoft article is really wrong and apparently has been wrong since 2007 (according to the doc date).
  • Wednesday, May 02, 2012 2:56 PM
    Moderator
     
     Answered Has Code

    Hi,

    VBScript requires the Set keyword in order to create an object reference (the reason for this is due to a bad design decision to allow the notion of default object properties, but I digress). Also, VBScript uses the & character to concatenate strings, not +. So the script should actually look like this:

    Dim WshShell, StrDesktop, oUrlLink
    Set WshShell = CreateObject("WScript.Shell")
    strDesktop = WshShell.SpecialFolders.Item("Desktop")
    Set oUrlLink = WshShell.CreateShortcut(strDesktop & "\Microsoft Web Site.url")
    oUrlLink.TargetPath = "http://www.microsoft.com"
    oUrlLink.Save()
    

    Bill

  • Wednesday, May 02, 2012 3:00 PM
     
     
    Hmmm... another intersting point:  despite the concatenation operator in VBS being & as Bill stated, The + does not throw an error in my code and does exactly what I want it to, where I want it to.  I'm so glad to be done with VBS -- PoSh is so nice.
  • Wednesday, May 02, 2012 3:04 PM
     
     
    So the MS article is wrong?  Shouldn't someone say something?

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

  • Wednesday, May 02, 2012 3:09 PM
     
     
    The MS article is really wrong.  It's not properly formatted to be executed in a WSF file or a VBS.  I would agree that it should be changed but have no idea who to tell or where to begin.  Haha.
  • Wednesday, May 02, 2012 3:11 PM
     
     
    The MS article is really wrong.  It's not properly formatted to be executed in a WSF file or a VBS.  I would agree that it should be changed but have no idea who to tell or where to begin.  Haha.

    Just post a comment at the bottom, correcting it.  (You'll probably get a bronze medal)

    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

  • Wednesday, May 02, 2012 3:16 PM
     
     
    I submitted feedback to the article but it's not like the newer stuff where everyone can see the comment.  I just submitted an anonymous message about the article but at least was potentially able to relay the message.