Change Target of a File Shortcut with VBA

1

I'm changing the name of several worksheets at the same time, but some of them have shortcuts, and in the middle of the change I want the shortcuts to accompany that change.

Sub changeTargetPath()

    Set wsc = WScript.CreateObject("WScript.Shell")
    Set Lnk = wsc.CreateShortcut(wsc.SpecialFolders("desktop") & "\tabela 1.lnk")

    Lnk.targetpath = """C:\Users\leandro.moreira\Desktop\tabela 1.xlsx"" C:\Users\leandro.moreira\Desktop\atalho 2.xlsx"
    Lnk.Description = "tabela 1"
    Lnk.Arguments = "C:\Users\leandro.moreira\Desktop\atalho 2.xlsx"
    Lnk.Save

End Sub

What I did code for is this.

    
asked by anonymous 17.05.2017 / 16:53

1 answer

0

I found the answer:

NOTE: You need to enable the Reference: Microsoft Shell Constrols And Automation

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:\endereço/do/desktop")  'Só a pasta, não o arquivo
    If Not folder Is Nothing Then
        Set folderItem = folder.ParseName("C:\pasta\do\atalho\atalho.lnk")   'Endereço do atalho
        If Not folderItem Is Nothing Then
            Set shortcut = folderItem.GetLink
            If Not shortcut Is Nothing Then
                shortcut.Path = "C:\destino\do\atalho"    'mude o destino do atalho
                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

I hope I have helped.

    
23.05.2017 / 15:03