lnk):
Object References | 531
Scripting and
Automation
Sub Shortcut(LinkFile, CommandLine)
Set WshShell = WScript.CreateObject("WScript.Shell")
If LCase(Right(LinkFile, 4)) <> ".lnk" And _
LCase(Right(LinkFile, 4)) <>".url" Then _
LinkFile = LinkFile & ".LNK"
Set ShortcutHandle = WshShell.CreateShortcut(LinkFile)
ShortcutHandle.TargetPath = CommandLine
ShortcutHandle.Save
End Sub
To create a shortcut to a program or file, use the following statement:
Call Shortcut("C:\Users\username\AppData\Roaming\Microsoft\Windows\
SendTo\Notepad.lnk", _"Notepad.exe")
To create a shortcut to an Internet address:
Call Shortcut("D:\Prjects\Important\Annoyances.url", _
"http://www.annoyances.org/")
If the first parameter, LinkFile, ends in .lnk (case doesn??™t matter), the
Shortcut subroutine automatically creates a standard Windows shortcut; if
LinkFile ends in .url, however, an Internet Shortcut file is created. Note the
If...Then structure in the routine, which automatically adds the .lnk filename
extension if no proper extension is found.
The LCase function, which transforms the contents of any
variable to lowercase, is vital here, as it completely compensates
for .URL, .url, .Url, and any other case mismatches in
the specified filename.
If you specify a nonexistent folder in the path for the new shortcut file, an
???Unspecified Error??? will occur. You may want to use the FolderExists function,
detailed in the ???How to Manipulate Files??? topic earlier in this chapter,
to supplement this routine and eliminate the possibility of this error.
Pages:
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739