I'm Michael Suodenjoki - a software engineer living in Kgs. Lyngby, north of Copenhagen, Denmark. This is my personal site containing my blog, photos, articles and main interests.
I'm Michael Suodenjoki - a software engineer living in Kgs. Lyngby, north of Copenhagen, Denmark. This is my personal site containing my blog, photos, articles and main interests.
Updated 2011.01.23 17:22 +0100 |
Ikke tilgængelig på Dansk
Version 1.0 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.
1 Introduction
2 Using ShellExecute
2.1 How
3 Conclusion
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.

» The action settings dialog in PowerPoint allows you to create actions on mouse click or mouse over, e.g. to call a program on mouse click.
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.