Navigation Menu Search PhysiologyWeb
PhysiologyWeb Logo Search PhysiologyWeb
   
— share —
Share on Facebook    Share on X    Share on LinkedIn    Share on Pinterest    Share on Reddit    Email    Copy URL
function CreateShortcut(DestinationFolder, TargetFile, LinkName, LinkDescription: String): Boolean;
var
IObject: IUnknown; //uses System unit
ISLink: IShellLink; //uses ShlObj unit
IPFile: IPersistFile; //uses ActiveX unit
LinkPath: String;
begin
CreateShortcut := False;

LinkPath := '';

try
IObject := CreateComObject(CLSID_ShellLink); //uses ComObj unit
ISLink := IObject as IShellLink;
IPFile := IObject as IPersistFile;

with ISLink do
begin
SetPath(PChar(TargetFile)); //uses ShlObj unit
SetWorkingDirectory(PChar(ExtractFilePath(TargetFile))); //uses ShlObj unit
If LinkDescription <> '' then SetDescription(PChar(LinkDescription)); //uses ShlObj unit
end;

LinkPath := DestinationFolder + '\' + LinkName;
LinkPath := ChangeFileExt(LinkPath, '.lnk');

IPFile.Save(PWChar(LinkPath), False); //uses ActiveX unit
IPFile.SaveCompleted(PWChar(LinkPath)); //uses ActiveX unit
except
LinkPath := '';
end;

If FileExists(LinkPath) = True then CreateShortcut := True else CreateShortcut := False;
end;

The procedure below uses the function shown above to create the shortcut link.

procedure TForm1.Button1(Sender: TObject);
begin
CreateShortcut('C:\PhysiologyWeb', 'C:\0\test.xlsx', 'test.xlsx', 'Excel file: test.xlsx');
end;

Often, it is useful to save the shortcut on the Windows desktop or in some other special folder. To obtain the location of Windows special folders, please see Get the location for Windows special folders (e.g., desktop, start menu, program files, fonts, AppData, etc.).





Posted: Monday, September 4, 2023
Last updated: Tuesday, March 18, 2025
— share —
Share on Facebook    Share on X    Share on LinkedIn    Share on Pinterest    Share on Reddit    Email    Copy URL