Resolving WScript shortcut to updated filename

Tommy Alatalo

New Member
Joined
Feb 12, 2017
Messages
28
I am creating a VBA script that loops through all .lnk (shortcuts) files in a folder, and inserts the filenames to a spreadsheet.
The problem I'm having with this is that once I create the shortcuts in Windows the shortcuts don't seem to update if I change the filename of the file that the shortcut points to.

Example of the behavior:
Step 1:
[folder with shortcuts]\ShortcutToFileA.lnk
[folder containing file]\FileA.pdf
Excel VBA inserts "FileA.pdf"

Step 2 (filename changes):
[folder with shortcuts]\ShortcutToFileA.lnk
[folder containing file]\FileA_Plus.pdf (renamed FileA.pdf)
Excel VBA still inserts "FileA.pdf"

So ShortcutToFileA.lnk still says "FileA" when getting the targetPath after renaming the file.

My Code:
Code:
For Each File In Folder.Files
                    Dim WSHShell As Object
                    Dim shortcut As Object
                    Set WSHShell = CreateObject("WScript.Shell")
                    With WSHShell.CreateShortcut(FileSystem.GetAbsolutePathName(File))
                    linkPath = .TargetPath
                    End With
**Use wordarray to get relevant sections of the filename to my spreadsheet**
Next

Is there a way to update or resolve the shortcut created in Windows explorer so that the VBA script gets the correct filename information every time even when the target filename changes?
The shortcut in windows explorer still points to the file "FileA_Plus.pdf" and opens correctly with Adobe Reader if I just change the name of that file, but for some reason the WScript shortcut doesn't see this change.
Help much appreciated!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
The thing here seems to be that the shortcuts actually do not update their target path until you double click it and open the target file.
This is quite strange as the shortcut targetpath can be pointing to "FileA.pdf", but when I double click it, it still manages to open "FileA_Plus.pdf" somehow, and after doing that the target path for the shortcut is updated to "FileA_Plus.pdf" as well.
So the question seems to be - what connection does that shortcut have to the file except the target path that allows it to find the file even though the target path is wrong? -And how do I use it in Excel VBA?
 
Upvote 0
Is there a way to update or resolve the shortcut created in Windows explorer so that the VBA script gets the correct filename information every time even when the target filename changes?
Try calling the Resolve method of the ShellLinkObject object (the shortcut) - see https://msdn.microsoft.com/en-us/office/bb774004(v=vs.100).

Resolve - Looks for the target of a Shell link, even if the target has been moved or renamed.

Change your code to something like this (play around with the Flags argument):
Code:
With WSHShell.CreateShortcut(FileSystem.GetAbsolutePathName(File))
    linkPath = .TargetPath
   .Resolve 1  'or 5?
End With
 
Upvote 0
Try calling the Resolve method of the ShellLinkObject object (the shortcut) - see https://msdn.microsoft.com/en-us/office/bb774004(v=vs.100).

Resolve - Looks for the target of a Shell link, even if the target has been moved or renamed.

Change your code to something like this (play around with the Flags argument):
Code:
With WSHShell.CreateShortcut(FileSystem.GetAbsolutePathName(File))
    linkPath = .TargetPath
   .Resolve 1  'or 5?
End With

Thanks for the tip, I was looking at that myself too, but if I insert the .Resolve = 1 (or whatever number) I get the error "Run-time error 438: Object doesn't support this propery or method".
Any idea why that might be?
 
Upvote 0
I don't understand your code because it is incomplete. What is Folder.Files? Why are you calling CreateShortcut if you are looping through existing shortcuts?

I think you should be using CreateObject("Shell.Application") (Microsoft Shell Controls and Automation), not CreateObject("wscript.shell") (Windows Script Host Object Model) to check shortcuts. Try this:

Code:
Public Sub Resolve_Shortcuts()

    Dim Sh32 As Object 'Shell32.Shell   'early binding data types require reference to Microsoft Shell Controls and Automation
    Dim ShFolder As Object 'Shell32.Folder
    Dim ShFolderItem As Object 'Shell32.FolderItem
    Dim shortcut As Object 'Shell32.ShellLinkObject
    Dim folderPath As String
        
    folderPath = "C:\path\to\folder"    'CHANGE TO YOUR FOLDER PATH
     
    Set Sh32 = CreateObject("Shell.Application")
    Set ShFolder = Sh32.Namespace((folderPath))  'extra parentheses required
    
    If Not ShFolder Is Nothing Then
    
        For Each ShFolderItem In ShFolder.Items
            If ShFolderItem.IsLink Then
                Set shortcut = ShFolderItem.GetLink
                shortcut.Resolve 5
                MsgBox "Shortcut: " & ShFolderItem.path & vbNewLine & "Target: " & shortcut.path
            End If
        Next
        
    Else
    
        MsgBox "Folder '" & folderPath & "' does not exist"
    
    End If
    
End Sub
 
Upvote 0
Folder.Files is a collection of files from a filesystembobject;

Code:
Set FileSystem = CreateObject("Scripting.FileSystemObject")
Set Folder = FileSystem.GetFolder(rootFolder)

Where rootFolder is the folder to be scanned for shortcuts. I just used the CreateShortcut method because it was used in some examples I found while searching for a solution online, it might be unnecessary with your way of using Shell32 instead.
I tried your script below, but it doesn't seem to resolve correctly either, it never finds the updated filename if I rename the target file. Did you have working results with it, I tried all different kind of .Resolve flags?


I don't understand your code because it is incomplete. What is Folder.Files? Why are you calling CreateShortcut if you are looping through existing shortcuts?

I think you should be using CreateObject("Shell.Application") (Microsoft Shell Controls and Automation), not CreateObject("wscript.shell") (Windows Script Host Object Model) to check shortcuts. Try this:

Code:
Public Sub Resolve_Shortcuts()

    Dim Sh32 As Object 'Shell32.Shell   'early binding data types require reference to Microsoft Shell Controls and Automation
    Dim ShFolder As Object 'Shell32.Folder
    Dim ShFolderItem As Object 'Shell32.FolderItem
    Dim shortcut As Object 'Shell32.ShellLinkObject
    Dim folderPath As String
        
    folderPath = "C:\path\to\folder"    'CHANGE TO YOUR FOLDER PATH
     
    Set Sh32 = CreateObject("Shell.Application")
    Set ShFolder = Sh32.Namespace((folderPath))  'extra parentheses required
    
    If Not ShFolder Is Nothing Then
    
        For Each ShFolderItem In ShFolder.Items
            If ShFolderItem.IsLink Then
                Set shortcut = ShFolderItem.GetLink
                shortcut.Resolve 5
                MsgBox "Shortcut: " & ShFolderItem.path & vbNewLine & "Target: " & shortcut.path
            End If
        Next
        
    Else
    
        MsgBox "Folder '" & folderPath & "' does not exist"
    
    End If
    
End Sub
 
Upvote 0
Resolve 1 and Resolve 5 work for me: before the Resolve call shortcut.path points to the previous name of the target file and after the Resolve call shortcut.path points to the renamed target file.

Change the code to output before and after values to the VBA immediate window:

Code:
Debug.Print "Target before Resolve: " & shortcut.path
                shortcut.Resolve 1
                Debug.Print "Target after Resolve: " & shortcut.path

Scenario: shortcut target file is "Sheet.pdf". Manually rename as "Sheet xx.pdf" in File Explorer. Run code and shortcut.path is "...\folder\Sheet.pdf" before Resolve and "...\folder\Sheet xx.pdf" after Resolve. Run code again and both before and after values are "...\folder\Sheet xx.pdf", proving that the .Resolve call has persisted the shortcut target.
 
Upvote 0
You're right, it does work now that I tried it again, thats great! Now I just need to implement this into my own code. Thank you!

Resolve 1 and Resolve 5 work for me: before the Resolve call shortcut.path points to the previous name of the target file and after the Resolve call shortcut.path points to the renamed target file.

Change the code to output before and after values to the VBA immediate window:

Code:
Debug.Print "Target before Resolve: " & shortcut.path
                shortcut.Resolve 1
                Debug.Print "Target after Resolve: " & shortcut.path

Scenario: shortcut target file is "Sheet.pdf". Manually rename as "Sheet xx.pdf" in File Explorer. Run code and shortcut.path is "...\folder\Sheet.pdf" before Resolve and "...\folder\Sheet xx.pdf" after Resolve. Run code again and both before and after values are "...\folder\Sheet xx.pdf", proving that the .Resolve call has persisted the shortcut target.
 
Upvote 0
There is actually another issue with this that I'm just discovering now that I'm trying to implement this. I put your code into my spreadsheet, but it doesn't resolve properly and Windows itself acts differently regarding the shortcuts now that I'm using them on a network mapped drive (or direct network path) to create and search for the shortcuts.


In Windows explorer I can create the shortcuts to the files and move them to location B. But if I now change the filename of the target file, Windows explorer itself cannot find it and I get an error message "The item file.pdf" that this shortcut refers to has been changed or moved, so this shortcut will no longer work properly. - Do you want to delete this shortcut?"
If Windows is unable to resolve shortcuts on a network path, are there any options to still make this work, or is this a dead end?


Resolve 1 and Resolve 5 work for me: before the Resolve call shortcut.path points to the previous name of the target file and after the Resolve call shortcut.path points to the renamed target file.


Change the code to output before and after values to the VBA immediate window:


Code:
Debug.Print "Target before Resolve: " & shortcut.path
                shortcut.Resolve 1
                Debug.Print "Target after Resolve: " & shortcut.path


Scenario: shortcut target file is "Sheet.pdf". Manually rename as "Sheet xx.pdf" in File Explorer. Run code and shortcut.path is "...\folder\Sheet.pdf" before Resolve and "...\folder\Sheet xx.pdf" after Resolve. Run code again and both before and after values are "...\folder\Sheet xx.pdf", proving that the .Resolve call has persisted the shortcut target.
 
Upvote 0
I can't really help further because I don't have a network path to test with.

Try different values for the Resolve argument or maybe you need to call the shortcut.Save method to save changes after shortcut.Resolve - see the page I linked to for details.
 
Upvote 0

Forum statistics

Threads
1,214,812
Messages
6,121,704
Members
449,048
Latest member
81jamesacct

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top