VBA Tutorial P4 – How to launch an app with VBA?

Hey there! Hope you’re having a great week!

How do you like VBA until now? Has it been any helpful for you? If it has, I’m sure you’ll enjoy this post as well!

As the title says, this post is about launching an app, any kind of app, with VBA.

Note: This is only for launching the app (e.g. opening it) and NOT about interacting with it! We’ll talk about interactions in another post.

Why would you need this, you might wonder? Well, you might need it if you want to integrate this function with some reminder VBA macro. Or maybe you want to set some programs to launch at startup and you are too lazy to add and/or remove shortcuts in/from the Startup folder (personally, this is what I’m using this for).

The code is very simple, but I want to add some explanations first. Remember when we discussed about Selenium? More specifically, about what is selenium and how can you use it? When we use selenium, we implement a third-party piece of software that is compatible with VBA so we can make use of it. Basically, we added an object that is not part of the main objects VBA has. And in case of selenium, the object wasn’t even in our computer so we can implement it, therefore we had to install it first.

Well, in case of launching an app with VBA, we also need to add an object that it is not part of the VBA main objects, but the object is now available in your computer.

Here is how the code looks like for launching Notepad:

‘Creating the object that is able to launch the app
Set objShell = CreateObject("WScript.Shell")
‘Launching the app from its location
objShell.Run ("""C:\Windows\notepad.exe""")

This works with shortcuts as well. Here is the line for running a Google Chrome shortcut placed on Desktop:

objShell.Run ("""C:\Users\" & LCase(Environ("username")) & "\Desktop\Google Chrome - Shortcut.lnk""")

It also works with launching documents, in case you need to work with multiple documents on a daily basis and you’re too lazy to open them one by one:

objShell.Run ("""C:\Users\" & LCase(Environ("username")) & "\Desktop\Doc.docx""")

That’s it! I hope it helps! Full code below!

Next -> VBA Tutorial P5 – How to connect a macro with a button?

Other posts in this series -> VBA Tutorial for Beginners


PS: If you want to support this website, you can do it through the Show Your Support page!


Sub launch_app()

Set objShell = CreateObject("WScript.Shell")

'Launching a program
objShell.Run ("""C:\Windows\notepad.exe""")

'Launching a shortcut
objShell.Run ("""C:\Users\" & LCase(Environ("username")) & "\Desktop\Google Chrome.lnk""")

'Launching a document
objShell.Run ("""C:\Users\" & LCase(Environ("username")) & "\Desktop\Doc.docx""")

End Sub

Leave a Reply