Library code snippets

Create a File Shortcut

1) Open a new exe standard proyect

2) Add one CommandButton, and two Textboxes, called cmdMakeLNKFile, txtTarget and txtLnkName respectively

3) Add the reference to the Windows Script Host Object Model (file: wshom.ocx, located in your system directory)

4) Copy this code to the Declarations sections:
       Dim Shell As WshShell
       Dim Shortcut As WshShortcut

5) Add this code to your form

Private Sub cmdMakeLNKFile_Click()

  'initialize the object WshShell
  Set objShell = New WshShell

  'initialize the object WshShortcut
  'the complete name of the .lnk file, include full path plus the .LNK file extension
  Set objShortcut = objShell.CreateShortcut (txtLnkName.Text)

  'the file to be called by the .lnk file, ej. "c:\windows\calc.exe"
  objShortcut.TargetPath = txtTarget.Text
 
  '(optional) := any command line supported by the file indicated in txtTarget.Text
  'objShortcut.Arguments = xxxx

  '(optional) : = a valid icon file : = To use the same icon of the target file, do not use the next line.
  'objShortcut.IconLocation = xxxx
 
  'Save the .lnk
  objShortcut.Save

End Sub

Comments

  1. 09 Sep 2007 at 01:37

    Hello,
         I have used your code and it has worked well for me till now. I have added the .Ocx and the Dim statements and all has gone well. However now that I run the program I recieve a COMException.

         The error number form the report says it's -2147467259. it is raised when I call Objshortcut.Save(),and the error reads "COMException was unhandled: Unable to save file: C:\...\...\Link.lnk

         Any Help Would Be Appreciated

    ~Vitus

  2. 09 Sep 2007 at 01:31

    Hello,
         I have used your code and had success adding in the Ocx and the Dim statements, However... When I run the application I recieve a ComException. the report goes somethinglike this, ComException was unhandled: unable to save "C:\...\...\...\...\Link.lnk"

         In the error report I retrived the Com error number which is -2147467259. the error occurs the first time I run this statement: objshortcut.Save()

         Any Help Would Be Appreciated

    ~Vitus

  3. 07 Dec 2003 at 12:04

    I'm using this code but sometimes i get the message "set objshell  doesn't declare" i've assign the "WSHOM.OCX" things and the "dim " things..
    BUT
    sometimes it works, but sometime it doesn't...  Help me please
  4. 17 Oct 2003 at 10:05

    Hello !


    When the shortcut is created, how can I read with VB or another language the value of arguments and targetpath ?


    Thanks !

  5. 31 May 2002 at 18:03

    This code comes in handy when creating a setup program the creates shortcuts to programs. I like it because it works with few lines of code. Thank you very much! I've been looking for this for a very long time!
    Happy programming!

  6. 01 Jan 1999 at 00:00

    This thread is for discussions of Create a File Shortcut.

Leave a comment

Sign in or Join us (it's free).

Carlos Icaza

Related discussion

Related podcasts

  • Christian Beauclair

    14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...

Want to stay in touch with what's going on? Follow us on twitter!