Supercharge your PowerPoint productivity with

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

Tell me about PPTools

ShellExecute Example


PPTools
Shape Styles brings the power of styles to PowerPoint. Apply complex formatting with a single click
Merge Excel, CSV or tab-delimited data into PowerPoint presentations to create certificates, awards presentations, personalized presentations and more
FixLinks prevents broken links when you distribute PowerPoint presentations
Optimizer saves disk space and bandwidth, shrinks your PowerPoint presentations to the right size for email, screenshow or printing
PPT2HTML gives you full control of PowerPoint HTML output, helps meet Section 508 accessibility requirements
Prep4PDF preserves interactivity in PowerPoint presentations when you convert to PDF
Image Export converts PowerPoint slides to JPG, PNG, GIF, WMF and more

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


Page copy protected against web site content infringement by Copyscape Contents © 1995-2008 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.

Español    Deutsch    Français    Português    Italiano    Nederlands    Greek    Japanese    Korean    Chinese



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.htm
Last update 09 September, 2006