Create a shortcut on user desktop using VB shell command


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Create a shortcut on user desktop using VB shell command

  1. #1
    Join Date
    Jan 2005
    Location
    USA
    Posts
    87

    Question Create a shortcut on user desktop using VB shell command

    Hi All

    How do I create a shortcut on user desktop using VB shell command?

    Can I use something like this in VB 6.0:
    ************************************************
    Set Shell = CreateObject("WScript.Shell")
    DesktopPath = Shell.SpecialFolders("Desktop")
    Set link = Shell.CreateShortcut(DesktopPath & "\test.lnk")
    link.Description = "test shortcut"
    link.TargetPath = "c:\blah\app.exe"
    link.WindowStyle = 3
    link.Save
    ************************************************

    Is WScript.Shell usually found on all user machines with Windows 2000 and XP.

    I dont want to install any OCXs on user machine,

    thanks
    vbdude

  2. #2
    Join Date
    Feb 2005
    Posts
    103
    Set C = CreateObject("WScript.Shell")
    DesktopPath = C.SpecialFolders("Desktop")
    Set link = C.CreateShortcut(DesktopPath & "\test.lnk")
    link.Description = "test shortcut"
    link.TargetPath = "c:\blah\app.exe"
    link.WindowStyle = 3
    link.Save

    You cant save as shell because its a vb thing i forget.

  3. #3
    Join Date
    Jan 2005
    Location
    USA
    Posts
    87
    Hi

    Is WScript.Shell a DLL/OCX that is found on ALL user machines with Windows 2000 and XP ??

    I dont want to install any OCXs on user machine.

    Also, do I need to include any References into my VB project?

    Thanks
    vbdude

  4. #4
    Join Date
    Feb 2005
    Posts
    103
    no you do not need to include any references or OCXS because windows 2000 and XP has wScript.shell.

    The wScript.Shell is located under system32 as description Microsoft (r) Windows Based Script Host.
    Last edited by XRsTX; 03-09-2005 at 07:33 PM.

  5. #5
    Join Date
    Jan 2005
    Location
    USA
    Posts
    87
    What if the shortcut already exist on user desktop ?

    How to delete the old one before addeing new one

    Thanks
    vbdude

  6. #6
    Join Date
    Jan 2005
    Location
    USA
    Posts
    87
    And is there any way of error checking?

    I mean, how do i make sure that shortcut was created successfully?

    Thanks
    vbdude

  7. #7
    Join Date
    Apr 2004
    Location
    New York City
    Posts
    538
    Quote Originally Posted by vbdude
    And is there any way of error checking?

    I mean, how do i make sure that shortcut was created successfully?

    Thanks
    vbdude
    There are 2 approaches I can think of:

    1) After the line Set link = C.CreateShortcut(DesktopPath & "\test.lnk"), test for whether link Is Nothing. This will tell you if the shortcut was created, but not whether it was created with the name & target you want, as the name & target setting happens later in the code.

    2) After all your code to create the shortcut runs, you could use the Dir$ function to check the existence of the .lnk file in the Desktop folder. Use code to determine the location of the Desktop folder. Since you're already using the Windows Scripting Host (set a reference to "Windows Script Host Object Model" first), this code will give you the full path to the Desktop folder:
    Code:
    Dim Wshl As WshShell
    Set Wshl = New WshShell
    
    Dim strLocation As String
    strLocation = Wshl.SpecialFolders("Desktop")
    You can now take the strLocation variable and use it with the Dir$ function- see VB's Help or http://www.msdn.com for details.



    -Andrew

  8. #8
    Join Date
    Jan 2005
    Location
    USA
    Posts
    87
    Great. Appreciate it.

    Thanks
    vbdude

  9. #9
    Join Date
    Jul 2007
    Posts
    2
    I have need useing the above script with GPO and it has been working great.
    My question is how to I use or edit the script to remove a desktop icon or shortcut.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center