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.

Article

Starting Programs from VBAUsing ShellExecute to start any program or shortcut from inside VBA

Updated 2014.01.06 18:59 +0100

Ikke tilgængelig på Dansk

By Michael Suodenjoki, michael@suodenjoki.dk.

Updated January 2014. Original version from November 2002.

Abstract

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.

Contents

Introduction
Using ShellExecute
  How
Conclusion

Introduction

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.

Illustrates the action settings dialog in PowerPoint which 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.

Using 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.

How

  1. Start up your VBA enabled program, e.g. PowerPoint and go into the Tools | Macros menu and select the 'Visual Basic Editor' menu item (or click Alt+F11). This should open up the VBA editor.
  2. Create a new module by right clicking on the Modules folder, and select Insert...| Module. The right hand side of the VBA editor should open up with an empty text editor.
  3. Enter the following piece of VB code, substituting the necessary parts to fit your purpose, e.g. rename the sub routine RunYourProgram to whatever you find most appropriate and remember to enter the right folder paths in the parameters to ShellExecute.

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
    
  1. Close the VBA editor when finished.
  2. Try out your new macro by activating Tools | Macros | Macros... which should open up the Macros dialog, allowing you to choose the macro to run.
  3. Select your new macro (e.g. named "RunYourProgram") and activate the Run button. If the Run button is not enabled your macro security level need first to be modified from inside Tools | Macros | Security... where you need to set the security level down to e.g. medium.

Conclusion

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.

Feedback

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