?
Solved

Shortcuts created with Windows Scripting Host dont link to files on LAN

Posted on 2006-04-26
19
Medium Priority
?
413 Views
Last Modified: 2008-02-01
Hi,
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
      ofS.CreateFolder (sPath)
End If

With rsFolders
    .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"
        oLInk.Save
       
        .MoveNext
    Loop
   
    .Close
End With
0
Comment
Question by:kkellymn
  • 10
  • 7
18 Comments
 
LVL 2

Expert Comment

by:LRDS
ID: 16550094
May be the link paths are different on the LAN Pc?
0
 

Author Comment

by:kkellymn
ID: 16551694
not sure what you mean, but I dont change the path when I go in and update it the actual link and it starts work

I can do something like changing the description or the icon and it will begin to work.
0
 
LVL 2

Expert Comment

by:vbStriker
ID: 16558915
If you have the ability to reach the target file via a network/mapped drive, I think your problem will go away.

I strongly suspect that it is a security based issue that relates to an unmapped UNC file reference.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:vbStriker
ID: 16558919
(*** Finishing my previous thought ***)

When you edit the shortcut, your permissions can be determined and ratified which is why I believe it starts to work at that point.
0
 

Author Comment

by:kkellymn
ID: 16559659
Thanks for the direction....can you think of anyway to get around this?  Is there any way to get the permissions ratified at the time of creation?

0
 
LVL 2

Expert Comment

by:vbStriker
ID: 16580436
Please confirm the programming language/environment so that I can run a quick test.
0
 

Author Comment

by:kkellymn
ID: 16580520
VBA within an Access MDB file
0
 
LVL 2

Expert Comment

by:vbStriker
ID: 16580894
Some of the declarations are not defined in a basic database.  Can you please let me know if you have specific referenced defined?

Also (if this is an option), a VBScript that works to create shortcuts is...

******************************************
Dim WSHShell, fs

Set WSHShell = WScript.CreateObject("WScript.Shell")

Set fs = WScript.CreateObject("Scripting.FileSystemObject")

Function MakeDesktopShortcut( name, target )

Dim Shortcut,DesktopPath,StartupPath

DesktopPath = WSHShell.SpecialFolders("Desktop")

Set Shortcut = WSHShell.CreateShortcut(DesktopPath & "\" & name & ".lnk")

Shortcut.TargetPath = target

StartupPath = fs.GetParentFolderName( target )

If fs.FolderExists( StartupPath ) then

   Shortcut.WorkingDirectory = StartupPath

End If

Shortcut.Save

End Function

MakeDesktopShortcut "Shortcut to local test file", "C:\test.txt"
MakeDesktopShortcut "Shortcut to remote test file", "\\192.168.1.1\work\test.txt"

******************************************

This file could be generated from your MS Access Module and then fired from the Module.
0
 
LVL 2

Expert Comment

by:vbStriker
ID: 16580982
Found the issue on my system... hope this works for you.  Note that this is your basic code with the database logic and variables removed.  It creates a link file in the root c: drive.

MSAccess Module
***************************************************
Option Compare Database

Option Explicit
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

    Set ows = CreateObject("WScript.Shell")
    sPath = "C:\"  'This folder will hold the links to the folders found in the underlying table of folders

    sLinkFile = "\\192.168.1.1\work\test.txt"
    ''sLinkFile = "C:\test.txt"
       
    Set oLInk = ows.CreateShortCut("C:\test.LNK")
    oLInk.TargetPath = sLinkFile
    oLInk.Description = "Link to C:\test.txt"
    oLInk.IconLocation = "C:\WINDOWS\system32\SHELL32.dll, 3"
    oLInk.WindowStyle = "1"
    oLInk.Save
       
End Sub

***************************************************
0
 

Author Comment

by:kkellymn
ID: 16581018
Hi,

I am not sure that I see anything that is different here.....
0
 
LVL 2

Expert Comment

by:vbStriker
ID: 16581233
Main difference is this line:
Set ows = CreateObject("WScript.Shell")

On my system this is where yours was failing.  I tested my code over a VPN, across my standard network (logged on to domain) and to a local file on my PC.

My understanding of WSH is that your definition/code should have failed in VBA but would have (mostly) worked in VBScript - which has more powerful features.

If you try my code and it still fails to create a working link then we are looking in the wrong spot.

Can you please confirm your network setup?  ie: Are you connected to a domain or workgroup, etc?
0
 

Author Comment

by:kkellymn
ID: 16581282
OK, now I see what was changed.

I had made a reference to the Windows Scripting Host in my project references so I was able to instantiate the object without using the CreateObject("....") step

see below...

Dim ows As WshShell
Dim oLInk As WshShortcut

Set ows = New WshShell

Network is on Domain with Active Directory.  I tried creating shortcuts using a PC logged onto the domain as well as a guest PC....remember that I am able to manually create shortcuts to network resources in both scenarios.
0
 
LVL 2

Expert Comment

by:vbStriker
ID: 16581385
Do you know if you have "Local Admin" rights on your PC when it is connected to the domain?
0
 

Author Comment

by:kkellymn
ID: 16581433
In both cases I am logging on as a user with Local Admin rights
0
 
LVL 2

Expert Comment

by:vbStriker
ID: 16581691
Without updating anything in the link that you create, can you please check the Security Tab and see what access the "EveryOne" user has?  On my shortcuts created with my code, "EveryOne" has every box ticked (ie: full security access).
0
 
LVL 2

Expert Comment

by:vbStriker
ID: 16581853
Sorry, but I don't know where else to look.  The only way I can find to create shortcuts from VB/VBA is as you have done.  I can't fault it so can only recommend that you try it from several other PC's and/or logons and see if the problem is profile or PC specific.
0
 

Author Comment

by:kkellymn
ID: 16771242
I guess I am not sure what the implication of not refunding the points is.  I have not recieved a valid response that solves the problem so I think it is OK to close out the question.
0
 
LVL 2

Accepted Solution

by:
vbStriker earned 375 total points
ID: 16772972
I have no problems with refunding the points.  I put a fair bit of time into it, but the name of the game is solving the problem - which didn't happen. :-(
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Introduction to Processes
Screencast - Getting to Know the Pipeline

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question