Updated 2014.01.06 18:59 +0100 |
Ikke tilgængelig på Dansk
Updated January 2014. Original version from November 2002.
This tiny article describes how you can use the Windows SDK function ShellExecute to start any program or shortcut (.lnk file) from inside your VBA (Visual Basic for Application) enabled application, e.g. the Microsoft Office applications.
If you just want to see how to use ShellExecute in VBA you may skip this section.
I was inside Microsoft PowerPoint and was making a presentation for a new product that I had to introduce for some of our customers. I was using the company PowerPoint template, nice and easy I thought. At a point I wanted a slide displaying the main window of the new product and I got the idea that it would be nice if I could start up the product by clicking somewhere on the slide, e.g. in a button or a hyperlink.
I first tried to create a button with a custom action - namely to startup the application. Hmm, that didn't work and after a while I found out that the macro security level has to be changed to allow macros to run in the presentation. So far so good. However there were just another slight problem - I wanted to start the application by calling a shortcut - or a file with the .lnk extension. The reason for this was that the shortcut controlled not only which program to startup, but also in which folder and the specific arguments to it.
However the custom action dialog didn't allow me to choose a shortcut (.lnk file) to execute. So what did I do?
A god idea is always to see how other people have solved the problem. So I tried to search on Google to see whether anybody had a good solution. I quickly realized that I had to use VBA to solve my problem. There were some newsgroup entries suggesting to use the VBA Shell Function, however none of my coding attempts worked with shortcuts. So finally I've decided to do it myself using a Windows SDK function that I knew would work, namely ShellExecute.
This section describes how you can use the Windows SDK function ShellExecute to start any program - including a shortcut (.lnk file) - from inside VBA. This can be used in any of your VBA enabled programs - typically any of the applications in the Windows Office series. I've used it inside PowerPoint in a slide to start up a shortcut to a program that I wanted to startup during the presentation.
The ShellExecute function is not a VBA function so we need to declare it first telling it where it can be found (in SHELL32.DLL) and which parameters it takes.
Const SW_SHOW = 1 Const SW_SHOWMAXIMIZED = 3 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 Sub RunYourProgram() Dim RetVal As Long On Error Resume Next RetVal = ShellExecute(0, "open", "<full path to program>", "<arguments>", _ "<run in folder>", SW_SHOWMAXIMIZED) End Sub
I hope that my solution to a very common problem have helped you in your daily work. Its frustrating when something that basically should be simple is difficult to accomplish. Using the Windows SDK function ShellExecute allows you to fully control which program to startup - and in my specifically example to allow me to call a shortcut (.lnk file) from inside PowerPoint during a presentation.
Hope you can use it.
Found your descriptions on shell execute. Explained so well, very usefull, Thank you, Gil, Canada, 2012-11-27
I found your article on using ShellExecute and would like to thank you for posting this. It has just helped me solve a problem that the VB Shell Function could not. The program would not start as it needed a start-up location which is not available using the VB Shell function. I sent a request for help to the software vendor and then found your solution and passed it on just in case it may help others trying to run their program. Thanks again and regards from Australia. John Finlay, Australia, 2007-04-19