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

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
kkellymnAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

LRDSCommented:
May be the link paths are different on the LAN Pc?
kkellymnAuthor Commented:
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.
vbStrikerCommented:
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.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

vbStrikerCommented:
(*** 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.
kkellymnAuthor Commented:
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?

vbStrikerCommented:
Please confirm the programming language/environment so that I can run a quick test.
kkellymnAuthor Commented:
VBA within an Access MDB file
vbStrikerCommented:
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.
vbStrikerCommented:
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

***************************************************
kkellymnAuthor Commented:
Hi,

I am not sure that I see anything that is different here.....
vbStrikerCommented:
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?
kkellymnAuthor Commented:
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.
vbStrikerCommented:
Do you know if you have "Local Admin" rights on your PC when it is connected to the domain?
kkellymnAuthor Commented:
In both cases I am logging on as a user with Local Admin rights
vbStrikerCommented:
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).
vbStrikerCommented:
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.
kkellymnAuthor Commented:
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.
vbStrikerCommented:
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. :-(

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.