Changing Shortcut Link with VBA

Coach3131

Board Regular
Joined
Nov 23, 2007
Messages
242
I have a shortcut file called LinkURL.url... how do I change the link (properties) of the file with VBA?
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number

Coach3131

Board Regular
Joined
Nov 23, 2007
Messages
242
How would you modify this code line to be able to change the shortcut target link before running? I am trying to download a csv and the link changes with dates that I want as variables

Code:
Shell "rundll32.exe shdocvw.dll,OpenURL " & (myDir & "\" & fn1), vbNormalFocus
 
Upvote 0

Coach3131

Board Regular
Joined
Nov 23, 2007
Messages
242
I am not an expert with messing with the shell, but I am able to run a shortcut... but how do I change the property of the file? I would imagine someone here would be knowledgeable on the subject.
 
Upvote 0

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,897
ADVERTISEMENT
Try this (edit the code where indicated) and tick MS Shell Controls and Automation in the VB editor under Tools - References.
Code:
'References Microsoft Shell Controls and Automation.
'http://msdn.microsoft.com/en-us/library/bb776890%28v=VS.85%29.aspx

Option Explicit

Public Sub Change_Shortcut()

    Dim shell As Shell32.shell
    Dim folder As Shell32.folder
    Dim folderItem As Shell32.folderItem
    Dim shortcut As Shell32.ShellLinkObject
    
    Set shell = New Shell32.shell
    
    Set folder = shell.Namespace("c:\folder\containing\the\shortcut")  'CHANGE THIS
    If Not folder Is Nothing Then
        Set folderItem = folder.ParseName("LinkURL.url")   'SHORTCUT FILE, AS PER YOUR FIRST POST
        If Not folderItem Is Nothing Then
            Set shortcut = folderItem.GetLink
            If Not shortcut Is Nothing Then
                shortcut.Path = "http://www.newurl.com/page.html"    'CHANGE THIS
                shortcut.Save
                MsgBox "Shortcut changed"
            Else
                MsgBox "Shortcut link within file not found"
            End If
        Else
            MsgBox "Shortcut file not found"
        End If
    Else
        MsgBox "Desktop folder not found"
    End If
    
End Sub
 
Upvote 0

donfaison

New Member
Joined
Jan 7, 2014
Messages
20
Office Version
  1. 365
Platform
  1. Windows
I have been "upgraded" and my username was changed. I am not an administrator so can't run a script. ALL of my shortcuts (probably a few hundred) need to be changed from
C:\users\oldname\... to c:\users\newname\...
Any suggestions?
 
Upvote 0

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
7,897
Please start a new thread, linking to this one if you think it would help. In the new thread tell us which folder(s) contain the shortcuts.
 
Upvote 0

Forum statistics

Threads
1,195,920
Messages
6,012,308
Members
441,690
Latest member
CyberWrek

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
Top