Create a shortcut on desktop

I’ve searched on internet for days and I got the codes to create a shortcut of a libreoffice file on desktop.
I’ll write it over here if anyone needs it.

Sub CreateShortCut
Dim WshShell As Object
Dim DeskTopPath As String
Dim sFile as String
Dim oShellLink as object
Dim sFullPath as String
GlobalScope.BasicLibraries.LoadLibrary("Tools")
WshShell = CreateObject("WScript.Shell")
DeskTopPath = WshShell.SpecialFolders.Item("Desktop")
sFullPath = ThisComponent.getLocation()
sFile = FileNameoutofPath(GetFileNameWithoutExtension(ThisComponent.getLocation()))
oShellLink = WshShell.CreateShortcut(DeskTopPath & "\" & sFile & ".lnk")
oShellLink.TargetPath = sFullPath
oShellLink.Save
End Sub

edit by @karolus escaped Code with Backtics

1 Like

@Jonjozz

Thank you for posting. It is better if you post a question and then place the code you have in an answer.

A couple of notes.

Your code has incorrect quote marks. These produce errors. See → Ask Discourse formatting

You do not give any information on use. For example this does error in Linux Ubuntu 20 using Writer. Would be nice to get further information.

Your Tag did note Calc so here is the error run in Calc:

Screenshot at 2021-10-03 21-26-54

I imagine this is for Windows?

1 Like

This post seems related → Macro to get values range of cells V22 to V221 (only not empty cells) and save it to txt file on desktop

The WScript.Shell is the necessary hint, as it usually addresses the Windows-Scripting-Host, so this should work with Win10 and I guess also with Win7.

Also the backslash in DeskTopPath & "\" & will only work with Windows (or MS-DOS).

the whole task is OS-related, and therfore complete offtopic on ask.libreoffice.org

1 Like