PDA

View Full Version : [SOLVED:] Create desktop shortcut



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

Jacob Hilderbrand
07-17-2005, 09:57 PM
Do you want to create a shortcut to test02.txt?


DesktopShortCut "C:\MyFolder\test02.txt"

Emily
07-17-2005, 10:12 PM
Not exactly text file, just example. May be *.ini

Thanks

Jacob Hilderbrand
07-17-2005, 10:20 PM
Right, so try change the line of code to the path and name of the file.


DesktopShortCut "C:\MyPath\FileName"

Emily
07-18-2005, 04:09 AM
That means we assume the file can be run by some application.

Thanks

Jacob Hilderbrand
07-18-2005, 09:09 AM
Correct, but that would have to do with the file associations in Windows. If the file is not associated with a program then you would get the Open With dialog when you tried to open it.