Public Sub CreateShortCutOnDeskTop(ByVal Name As String, ByVal Description As String)
Dim x As New IWshRuntimeLibrary.IWshShell_Class
Dim y As IWshRuntimeLibrary.IWshShortcut_Class
Set y = x.CreateShortcut(x.SpecialFolders.Item("AllUsersDesktop") & "\" & Name & ".lnk")
'¿ì½Ý·½Ê½ÊôÐÔÖеÄÄ¿±ê
y.TargetPath = App.Path & "\ÏîÄ¿¹ÜÀíϵͳ.exe"
'¿ì½Ý·½Ê½ÊôÐÔÖеı¸×¢
y.Description = Description
'¿ì½Ý·½Ê½ÊôÐÔÖÐµÄÆðʼλÖÃ
y.WorkingDirectory = App.Path
y.Save
End Sub
Public Sub CreateShortCutOnStartMenu(ByVal Name As String, ByVal Description As String)
Dim x As New IWshRuntimeLibrary.IWshShell_Class
Dim z As IWshRuntimeLibrary.IWshShortcut_Class
Set z = x.CreateShortcut(x.SpecialFolders.Item("AllUsersStartMenu") & "\³ÌÐò\" & Name & ".lnk")
'¿ì½Ý·½Ê½ÊôÐÔÖеÄÄ¿±ê
z.TargetPath = App.Path & "\ÏîÄ¿¹ÜÀíϵͳ.exe"
'¿ì½Ý·½Ê½ÊôÐÔÖеı¸×¢
z.Description = Description
'¿ì½Ý·½Ê½ÊôÐÔÖÐµÄÆðʼλÖÃ
z.WorkingDirectory = App.Path
z.Save
End Sub
Public Sub main()
Call CreateShortCutOnDeskTop("ÏîÄ¿¹ÜÀíϵͳ", "")
Call CreateShortCutOnStartMenu("ÏîÄ¿¹ÜÀíϵͳ", "")
End Sub
关于"快捷方式"的几个问题 (引用 Windows Script Host Model):
1.如何获取某一已存在的快捷方式(*.lnk)的所有信息?
Dim x As New IWshRuntimeLibrary.IWshShell_Class
Dim y As IWshRuntimeLibrary.IWshShortcut_Class
Set y = x.CreateShortcut("..\..\XXX.lnk")
Dim s As String
s = "Arguments: " & y.Arguments & vbCrLf _
& "Description: " & y.Description & vbCrLf _
& "FullName: " & y.FullName & vbCrLf _
& "Hotkey: " & y.Hotkey & vbCrLf _
& "IconLocation: " & y.IconLocation & vbCrLf _
& "TargetPath: " & y.TargetPath & vbCrLf _
& "WindowStyle: " & y.WindowStyle & vbCrLf _
& "WorkingDirectory: " & y.WorkingDirectory
MsgBox s
2.如何在 Windows 任务栏中创建快捷方式?
Dim x As New IWshRuntimeLibrary.IWshShell_Class
Dim y As IWshRuntimeLibrary.IWshShortcut_Class
If VBA.Len(VBA.Trim(VBA.Dir(x.SpecialFolders.Item("AppData") & "\Microsoft\Internet Explorer\Quick Launch\"))) > 0 Then
Set y = x.CreateShortcut(x.SpecialFolders.Item("AppData") & "\Microsoft\Internet Explorer\Quick Launch\WinRAR.lnk")
y.TargetPath = "..\..\XXX.exe"
y.Save
End If
'下面是几种 Windows 特殊路径:
AllUsersDesktop
AllUsersStartMenu
AllUsersPrograms
AllUsersStartup
AppData
Desktop
Favorites
Fonts
MyDocuments
NetHood
PrintHood
Programs
Recent
SendTo
StartMenu
Startup
Templates
综上 CreateShortcut 是用来创建一个 WshShortcut 的对象, 只要不调用其 Save 方法,就不会真正改变快捷方式的属性。
'创建快捷方式
Public Sub CreateShortCutOnDeskTop(ByVal Name As String, ByVal Description As String, TargetPath As String, WorkingDirectory As String)
Dim X As New IWshRuntimeLibrary.IWshShell_Class
Dim Y As IWshRuntimeLibrary.IWshShortcut_Class
'Attribute VB_Name = "ShortCut"
Set Y = X.CreateShortcut(X.SpecialFolders.item("AllUsersDesktop") & "\" & Name & ".lnk")
Y.TargetPath = TargetPath
Y.Description = Description
Y.WorkingDirectory = WorkingDirectory
Y.Save
End Sub