ShellExecute Example
If you want to launch a document file of some sort, you can use VB/VBA's SHELL command, but that requires you to know the full path to the executable (ie the program to launch the document IN) as well as the name of the document.
That's likely to vary from one Windows computer to another, so you can't be sure that a hardcoded path will work on every computer.
If you use ShellExecute instead, your problems are pretty much solved for you. ShellExecute is the code equivalent of a user doubleclicking a file icon. It causes Windows to work out what application the document file is associated with, launch the program and have it load the document file.
By using ShellExecute, you don't need to know the name or location of the program that's registered to a particular file type. Windows takes care of that for you. For example, you can ShellExecute a .PDF file and, so long as Reader, Acrobat or some other PDF-reading app is installed, Windows will launch it and load the PDF for you.
Here's how:
In the declarations section of your code, add:
Public Declare Function ShellExecute _ Lib "shell32.dll" _ Alias "ShellExecuteA" ( _ ByVal hwnd As Long, _ ByVal lpOperation As String, _ ByVal lpFile As String, _ ByVal lpParameters As String, _ ByVal lpDirectory As String, _ ByVal nShowCmd As Long) _ As Long
Then to use ShellExecute use code like so:
Sub ShellExec() Dim strFile As String Dim strAction as String Dim lngErr As Long ' Edit this: strFile = "c:\somefile.txt" ' the file you want to open/etc. strAction = "OPEN" ' action might be OPEN, NEW or other, depending on what you need to do lngErr = ShellExecute(0, strAction, strFile, "", "", 0) ' optionally, add code to test lngErr End Sub
You can find more detailed information and example code at ShellExecute Madness on Randy Birch's VBNet site.
See How do I use VBA code in PowerPoint? to learn how to use this example code.
Search terms:shell,shellexecute,launch,run