Supercharge your PowerPoint productivity with
Supercharge your PPT Productivity with PPTools - Click here to learn more.

Proud member of

PPTools

Image Export converts PowerPoint slides to high-quality images.

PPT2HTML exports HTML even from PowerPoint 2010 and 2013, gives you full control of PowerPoint HTML output, helps meet Section 508 accessibility requirements

Merge Excel data into PowerPoint presentations to create certificates, awards presentations, personalized presentations and more

Resize your presentations quickly and without distortion

Language Selector switches the text in your presentation from one language to another

FixLinks prevents broken links when you distribute PowerPoint presentations

Shape Styles brings styles to PowerPoint. Apply complex formatting with a single click.

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


Did this solve your problem? If so, please consider supporting the PPT FAQ with a small PayPal donation.
Page copy protected against web site content infringement by Copyscape Contents © 1995 - 2022 Stephen Rindsberg, Rindsberg Photography, Inc. and members of the MS PowerPoint MVP team. You may link to this page but any form of unauthorized reproduction of this page's contents is expressly forbidden.

Supercharge your PPT Productivity with PPTools

content authoring & site maintenance by
Friday, the automatic faq maker (logo)
Friday - The Automatic FAQ Maker

ShellExecute Example
http://www.pptfaq.com/FAQ00479_ShellExecute_Example.htm
Last update 07 June, 2011
Created: