Results 1 to 5 of 5

Thread: VB - The simplest way to Create Shortcut

  1. #1

    Thread Starter
    Lively Member WindowsNuclear's Avatar
    Join Date
    Oct 2002
    Posts
    106

    Cool VB - The simplest way to Create Shortcut

    This maybe the simplest way to Create Shortcut. After all, it require Win98 se or higher, beacause it use the vbscript to do the job.
    Now, there is still something to explain.
    The ShortCutPath must be the special folders in your system. I have tried out the follows:
    "Desktop :Our Desktop
    Programs :Start Menu\Programs
    StartMenu :Start Menu
    StartUp :Start Menu\Programs\StartUp
    SendTo :Windows\SentTo
    Fonts :Windows\Fonts
    Favorites :Windows\Favorites"

    The Window_Style is a integer,
    Window_Style=3 means MaximizedWindows when run.
    Window_Style=7 means MinimizedWindows when run.
    Well, Others means NomalWindows when run.
    Default is 0.

    The IconNum set our Shortcut's icon.
    0 means the first icon in the target file.
    1 the sencond ...
    2 the third ...
    and so on.
    Default is 0.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4. 'This will Create a ShortCut of Notepad in our desktop, its name is "Notepad", minimize windows when run, use the 2nd icon as the Shortcut icon.
    5.  
    6. Create_ShortCut "C:\WINDOWS\NOTEPAD.EXE", "Desktop", "Notepad", , 7, 1
    7. End Sub
    8.  
    9. Sub Create_ShortCut(ByVal TargetPath As String, ByVal ShortCutPath As String, ByVal ShortCutname As String, Optional ByVal WorkPath As String, Optional ByVal Window_Style As Integer, Optional ByVal IconNum As Integer)
    10.  
    11. Dim VbsObj As Object
    12. Set VbsObj = CreateObject("WScript.Shell")
    13.  
    14. Dim MyShortcut As Object
    15. ShortCutPath = VbsObj.SpecialFolders(ShortCutPath)
    16. Set MyShortcut = VbsObj.CreateShortcut(ShortCutPath & "\" & ShortCutname & ".lnk")
    17. MyShortcut.TargetPath = TargetPath
    18. MyShortcut.WorkingDirectory = WorkPath
    19. MyShortcut.WindowStyle = Window_Style
    20. MyShortcut.IconLocation = TargetPath & "," & IconNum
    21. MyShortcut.Save
    22.  
    23. End Sub
    Program is a pleasure thing!

  2. #2
    Fanatic Member ididntdoit's Avatar
    Join Date
    Apr 2006
    Location
    :uoıʇɐɔoן
    Posts
    765

    Lightbulb Re: VB - The simplest way to Create Shortcut

    Sorry for trying to revive an old thread, but how would I add a command line argument? I tried
    VB Code:
    1. MyShortcut.TargetPath = TargetPath & " " & Arguments

    but this automatically puts quotes around the entire string I fed it, so it thinks the agument is part of the file path and it screws up. Anyone?
    Visit here to learn to make the VB interface fit you!.
    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
    "The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners" -- Ernst Jan Plugge

  3. #3
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: VB - The simplest way to Create Shortcut

    one of the reasons early binding is useful:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Create_ShortCut "C:\WINDOWS\NOTEPAD.EXE", "Desktop", "Notepad", "test arg", , WshNormalFocus
    3. End Sub
    4.  
    5. Private Sub Create_ShortCut(ByVal sTargetPath As String, ByVal sShortCutPath As String, ByVal sShortCutName As String, _
    6.                             Optional ByVal sArguments As String, Optional ByVal sWorkPath As String, _
    7.                             Optional ByVal eWinStyle As WshWindowStyle = vbNormalFocus, Optional ByVal iIconNum As Integer)
    8.     ' Requires reference to Windows Script Host Object Model
    9.     Dim oShell As IWshRuntimeLibrary.WshShell
    10.     Dim oShortCut As IWshRuntimeLibrary.WshShortcut
    11.    
    12.     Set oShell = New IWshRuntimeLibrary.WshShell
    13.     Set oShortCut = oShell.CreateShortcut(oShell.SpecialFolders(sShortCutPath) & _
    14.                                           "\" & sShortCutName & ".lnk")
    15.     With oShortCut
    16.         .TargetPath = sTargetPath
    17.         .Arguments = sArguments
    18.         .WorkingDirectory = sWorkPath
    19.         .WindowStyle = eWinStyle
    20.         .IconLocation = sTargetPath & "," & iIconNum
    21.         .Save
    22.     End With
    23.    
    24.     Set oShortCut = Nothing: Set oShell = Nothing
    25. End Sub

  4. #4
    Junior Member
    Join Date
    Jun 2008
    Posts
    24

    Re: VB - The simplest way to Create Shortcut

    Well i've tryed using this but i keep getting this error



    and this line gets highlighted
    Set VbsObj = CreateObject("WScript.Shell")

    any suggestions please

  5. #5
    Member
    Join Date
    Feb 2011
    Posts
    54

    Re: VB - The simplest way to Create Shortcut

    how to do this on vista and 7 with admin rights check on the Advanced button

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width