• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

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
0
kkellymn
Asked:
kkellymn
  • 10
  • 7
1 Solution
 
LRDSCommented:
May be the link paths are different on the LAN Pc?
0
 
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.
0
 
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.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
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.
0
 
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?

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

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

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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 10
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now