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:
Option Explicit
Private Sub Command1_Click()
'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.
Create_ShortCut "C:\WINDOWS\NOTEPAD.EXE", "Desktop", "Notepad", , 7, 1
End Sub
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)
Dim VbsObj As Object
Set VbsObj = CreateObject("WScript.Shell")
Dim MyShortcut As Object
ShortCutPath = VbsObj.SpecialFolders(ShortCutPath)
Set MyShortcut = VbsObj.CreateShortcut(ShortCutPath & "\" & ShortCutname & ".lnk")
MyShortcut.TargetPath = TargetPath
MyShortcut.WorkingDirectory = WorkPath
MyShortcut.WindowStyle = Window_Style
MyShortcut.IconLocation = TargetPath & "," & IconNum
MyShortcut.Save
End Sub
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:
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?
Re: VB - The simplest way to Create Shortcut
one of the reasons early binding is useful:
VB Code:
Private Sub Command1_Click()
Create_ShortCut "C:\WINDOWS\NOTEPAD.EXE", "Desktop", "Notepad", "test arg", , WshNormalFocus
End Sub
Private Sub Create_ShortCut(ByVal sTargetPath As String, ByVal sShortCutPath As String, ByVal sShortCutName As String, _
Optional ByVal sArguments As String, Optional ByVal sWorkPath As String, _
Optional ByVal eWinStyle As WshWindowStyle = vbNormalFocus, Optional ByVal iIconNum As Integer)
' Requires reference to Windows Script Host Object Model
Dim oShell As IWshRuntimeLibrary.WshShell
Dim oShortCut As IWshRuntimeLibrary.WshShortcut
Set oShell = New IWshRuntimeLibrary.WshShell
Set oShortCut = oShell.CreateShortcut(oShell.SpecialFolders(sShortCutPath) & _
"\" & sShortCutName & ".lnk")
With oShortCut
.TargetPath = sTargetPath
.Arguments = sArguments
.WorkingDirectory = sWorkPath
.WindowStyle = eWinStyle
.IconLocation = sTargetPath & "," & iIconNum
.Save
End With
Set oShortCut = Nothing: Set oShell = Nothing
End Sub
Re: VB - The simplest way to Create Shortcut
Well i've tryed using this but i keep getting this error
http://img529.imageshack.us/img529/8017/42146149db5.jpg
and this line gets highlighted
Set VbsObj = CreateObject("WScript.Shell")
any suggestions please :(
Re: VB - The simplest way to Create Shortcut
how to do this on vista and 7 with admin rights check on the Advanced button