Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically create at shortcut to a file

Tags:

excel

vba

I have a small piece of code under a command button click which saves the workbook file with a new name in a new location, I am wondering if it is possible to also automatically create a shortcut to that newly saved workbook in a different location?

Private Sub CommandButton1_Click()
Dim SelectedFNumber As String
Dim DateStr As String
Dim myFileName As String
Dim StorePath As String

    DateStr = Format(Now, "dd.mm.yy HH.mm")

    SelectedFNumber = Range("B4").Text

    If SelectedFNumber <> "SELECT F NUMBER" And Range("D11") > "0" Then

        StorePath = "G:\Targets\" & SelectedFNumber & "\"

        myFileName = StorePath & SelectedFNumber & " " & DateStr & ".xlsm

        If Len(Dir(StorePath, vbDirectory)) = 0 Then
        MkDir StorePath
        End If

        ActiveWorkbook.SaveAs Filename:=myFileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
    Else
        MsgBox "Select an F Number"
    End If
End Sub
like image 200
J.Goddard Avatar asked Sep 08 '25 11:09

J.Goddard


1 Answers

You basically need to add something like this:

Dim sShortcutLocation As String

sShortcutLocation = "C:\blah\workbook shortcut.lnk"

With CreateObject("WScript.Shell").CreateShortcut(sShortcutLocation)
    .TargetPath = myFileName
    .Description = "Shortcut to the file"
     .Save
End With

changing the location to wherever you want.

like image 59
Rory Avatar answered Sep 11 '25 10:09

Rory