Shortcuts created with Windows Scripting Host dont link to files on LAN
Posted on 2006-04-26
I am using the following code to create a shortcut to a folder programtically.
When the target folder is on my PC, the shortcut works without a problem. However, when the target is a folder on the LAN, the shortcut is created, but I need to go into the shortcut properties and make a change (ie, change icon, change the description - it does not seem to matter which change I make) in order for the shortcut to become functional. I am guessing this is because windows can't verify that the file is there. When I double click the shortcut before I have made a change to the properties, I am prompted to select an application to use to open the file. The file is created with the *.lnk extension, all properties seem to be OK, for some reason it just doesnt work until I make a modification.
Here is the code - I modified it from my code to make names of fields generic, etx.
Private Sub CreateShortCut()
Dim sLinkFile As String
Dim oFS As Scripting.FileSystemObject
Dim rsFolders As ADODB.Recordset
Dim sSQL_Folders As String
Dim ows As WshShell
Dim oLInk As WshShortcut
dim sPath as string
sSQL_Folders = "qryFolders"
Set ows = New WshShell
Set rsFolders = New ADODB.Recordset
Set oFS = New Scripting.FileSystemObject
sPath = "C:\Links Test" 'This folder will hold the links to the folders found in the underlying table of folders
If ofS.FolderExists(sPath) = False Then
.Open sSQL_Folders, CurrentProject.Connection, adOpenDynamic, adLockPessimistic
Do While Not .EOF
'This loops through a table that has a bunch of records referencing the full path of some folders
sTargetFileName = !FolderName
sLinkFile = "\\Some Server\Some Folder\" & sTargetFileName & ".LNK"
'sLinkFile = "C:\Some Folder \" & sTargetFileName & ".LNK"
Set oLInk = ows.CreateShortCut(sPath & "\" & sTargetFileName & ".LNK")
oLInk.TargetPath = sLinkFile
oLInk.Description = "Link to " & sTargetFileName
oLInk.IconLocation = "C:\WINDOWS\system32\SHELL32.dll, 3"
oLInk.WindowStyle = "1"