Change Destination of a File Shortcut with VBA

Asked

Viewed 193 times

1

I am 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 follow this 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 made of code is this.

  • Hello, friend! Do you know where the shortcuts you would like to update are? If yes, it is easier to help you. I say this because there is no quick way to find all shortcuts linked to a particular file on the system.

  • I know how to find yes. The problem is to change.

1 answer

0


I found the answer:

NOTE: It is necessary to activate 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’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.