Emily
07-17-2005, 09:22 PM
The code below can create shortcut in desktop. But it cannot create shortcut link like "c:\windows\notepad.exe test02.txt".
Can it be modified not using any BAT file ("c:\windows\notepad.exe test02.txt") ?
' Reference: Windows Script Host Object Model
Sub SetLink()
Dim lnk As Object
DesktopShortCut "c:\windows\notepad.exe"
'DesktopShortCut "c:\windows\notepad.exe test02.txt"
End Sub
Function DesktopShortCut(strFullFilePathName As String) As Long
Dim WSHShell As IWshRuntimeLibrary.IWshShell_Class
Dim WSHShortcut As IWshRuntimeLibrary.IWshShortcut_Class
Dim strDesktopPath As String, workingdirectory As String
Dim strFileName As String
Dim strPath As String
On Error GoTo ErrHandler
Set WSHShell = New IWshRuntimeLibrary.IWshShell_Class
strFileName = Dir(strFullFilePathName)
strPath = Left(strFullFilePathName, _
Len(strFullFilePathName) - Len(strFileName))
If Not Len(strFileName) = 0 Then
strDesktopPath = WSHShell.SpecialFolders.Item("Desktop")
Set WSHShortcut = WSHShell.CreateShortcut _
(strDesktopPath & "\" & strFileName & ".lnk")
With WSHShortcut
.TargetPath = WSHShell. _
ExpandEnvironmentStrings(strFullFilePathName)
workingdirectory = WSHShell. _
ExpandEnvironmentStrings(strPath)
.WindowStyle = 4
.IconLocation = WSHShell. _
ExpandEnvironmentStrings(Application.Path _
& "\notepad.exe , 0")
.Save
End With
DesktopShortCut = 1
Else
DesktopShortCut = 0
End If
Continue:
Set WSHShell = Nothing
Exit Function
ErrHandler:
DesktopShortCut = -1
Resume Continue
End Function
Can it be modified not using any BAT file ("c:\windows\notepad.exe test02.txt") ?
' Reference: Windows Script Host Object Model
Sub SetLink()
Dim lnk As Object
DesktopShortCut "c:\windows\notepad.exe"
'DesktopShortCut "c:\windows\notepad.exe test02.txt"
End Sub
Function DesktopShortCut(strFullFilePathName As String) As Long
Dim WSHShell As IWshRuntimeLibrary.IWshShell_Class
Dim WSHShortcut As IWshRuntimeLibrary.IWshShortcut_Class
Dim strDesktopPath As String, workingdirectory As String
Dim strFileName As String
Dim strPath As String
On Error GoTo ErrHandler
Set WSHShell = New IWshRuntimeLibrary.IWshShell_Class
strFileName = Dir(strFullFilePathName)
strPath = Left(strFullFilePathName, _
Len(strFullFilePathName) - Len(strFileName))
If Not Len(strFileName) = 0 Then
strDesktopPath = WSHShell.SpecialFolders.Item("Desktop")
Set WSHShortcut = WSHShell.CreateShortcut _
(strDesktopPath & "\" & strFileName & ".lnk")
With WSHShortcut
.TargetPath = WSHShell. _
ExpandEnvironmentStrings(strFullFilePathName)
workingdirectory = WSHShell. _
ExpandEnvironmentStrings(strPath)
.WindowStyle = 4
.IconLocation = WSHShell. _
ExpandEnvironmentStrings(Application.Path _
& "\notepad.exe , 0")
.Save
End With
DesktopShortCut = 1
Else
DesktopShortCut = 0
End If
Continue:
Set WSHShell = Nothing
Exit Function
ErrHandler:
DesktopShortCut = -1
Resume Continue
End Function