Open Application and Pass Parameters
It is often useful to open an application programmatically and pass one or more parameters (or arguments) to it at the same time at startup. This may be done to control the behavior/mode of the application, or how the application opens and operates on a certain file. Parameters can be passed as part of the application's startup command. These are generally referred to as command-line switches. Command-line switches are unique to each application. For example, command-line switches that work for Microsoft Word will not work for other software applications, such as Adobe Acrobat, or even Microsoft Excel or Microsoft PowerPoint. If no parameters are passed to the application at startup, the application opens in its normal or default mode.
Here is a simple Delphi procedure that takes two parameters as its input. The first input is the path of the application executable file, and the second parameter includes appropriate command-line switches for that application. The path to the executable file may be absolute, or it may simply be the executable short name of a properly installed application. Most popular software applications provide guidance regarding command-line switches that can be passed to the program. For example, see
here for acceptable command-line switches for Microsoft Word, Excel, PowerPoint and PowerPoint Viewer, Outlook, and Access. Below are some code examples of using this procedure with command-line switches.
Microsoft Word
Each of the following two lines of code starts Microsoft Word and opens the file noted.
OpenApplicationPassParameters('WINWORD.EXE', 'C:\file.docx');
or
OpenApplicationPassParameters('WINWORD.EXE', '/t C:\file.docx');
The following starts Microsoft Word and opens the two files noted.
OpenApplicationPassParameters('WINWORD.EXE', 'C:\file1.docx C:\file2.docx');
The following starts Microsoft Word without displaying the Word start splash screen and opens the file noted.
OpenApplicationPassParameters('WINWORD.EXE', '/q C:\file.docx');
The following starts a new instance of Microsoft Word and opens a blank document.
OpenApplicationPassParameters('WINWORD.EXE', '/w');
The following starts Microsoft Word in safe mode.
OpenApplicationPassParameters('WINWORD.EXE', '/safe');
Microsoft Excel
The following starts Microsoft Excel and opens the file noted.
OpenApplicationPassParameters('EXCEL.EXE', 'C:\file.xlsx');
The following starts Microsoft Excel and opens the two files noted.
OpenApplicationPassParameters('EXCEL.EXE', 'C:\file1.xlsx C:\file2.xlsx');
The following starts Microsoft Excel and opens the file noted in read-only mode.
OpenApplicationPassParameters('EXCEL.EXE', '/r C:\file.xlsx');
Microsoft PowerPoint
The following starts Microsoft PowerPoint and opens the noted file.
OpenApplicationPassParameters('POWERPNT.EXE', 'C:\file.pptx');
The following starts Microsoft PowerPoint and opens the noted file in slideshow mode.
OpenApplicationPassParameters('POWERPNT.EXE', '/s C:\file.pptx');
Adobe Acrobat
The following starts Adobe Acrobat and opens the file noted.
OpenApplicationPassParameters('Acrobat.exe', 'C:\file.pdf');
The following starts a new instance of Adobe Acrobat (even if one is already open) and opens the file noted.
OpenApplicationPassParameters('Acrobat.exe', '/n C:\file.pdf');
The following starts Adobe Acrobat and opens the two files noted.
OpenApplicationPassParameters('Acrobat.exe', 'C:\file1.pdf C:\file2.pdf');
The following starts Adobe Acrobat, opens the noted file, and goes to the print dialog.
OpenApplicationPassParameters('Acrobat.exe', '/p C:\file.pdf');
The following starts Adobe Acrobat, opens the noted file, and zooms to 300%.
OpenApplicationPassParameters('Acrobat.exe', '/a zoom=300 C:\file.pdf');
This procedure is very useful when there is a desire to open an application and control its behavior/mode. The procedure eliminates the need to remember the details of the ShellExecute function (which calls the
ShellExecuteA function within the Windows API). For additional Delphi code for the Windows operating system, see
Useful Delphi Code for the Windows Operating System.
procedure OpenApplicationPassParameters(ApplicationPath, Parameters: String);
begin
ShellExecute(Application.Handle, PChar('open'), PChar(ApplicationPath), PChar(Parameters), nil, SW_SHOWNORMAL);
//references
//https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea
//https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
end;
Posted: Saturday, May 27, 2023