Launching by Document Extension (w/ ShellExecute)

PWNettle
06-19-2001, 10:03 AM
You can launch a document, email address, or URL based on the extension or structure of the document name (in the case of URLs and email addresses) using the ShellExecute API call. This allows you to simulate the double-click or 'run' action performed by the operating system. For example, if you double-click on a Word document in Windows Explorer or on your desktop Windows automatically launches the Word document into Microsoft Word. Using the ShellExecuteAPI you can launch a URL in the default browser, open a new email message with the default email client, or launch any document based on it's extension.

To use the ShellExecute API you must declare it. If you are using inside a form you can declare it as private for use in that form only in the general declarations section of the form. For example:
Private 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 LongIf you want to use the ShellExecute API from multiple forms you could declare it (without the Private keyword) in a code module (.BAS file).

Here are some examples of using the ShellExecute API.

To lauch a URL in the default browser:
Dim strURL As String
Dim lngResult As Long

strURL = "http://www.visualbasicforum.com/bbs/wwwthreads.php?Cat="

lngResult = ShellExecute(Me.hwnd, "Open", strURL, "", "", vbNormalFocus)To launch an email address in the default email client:
Dim strEmail As String
Dim lngResult As Long

strEmail = "mailto:somebody@somedomain.com"

lngResult = ShellExecute(Me.hwnd, "Open", strEmail, "", "", vbNormalFocus)To launch a text file into the default text editor (usually this is Notepad):
Dim strFilename As String
Dim lngResult As Long

strFilename = App.Path & "\test.txt"

lngResult = ShellExecute(Me.hwnd, "Open", strFilename, "", "", vbNormalFocus)To launch a Word document into MS Word:
Dim strFilename As String
Dim lngResult As Long

strFilename = App.Path & "\test.doc"

lngResult = ShellExecute(Me.hwnd, "Open", strFilename, "", "", vbNormalFocus)These examples are pretty simple. If you have specific questions regarding the use of the ShellExecute API I would suggest posting them to the general forum.

Cheers,
Paul


Fixed pauls old pre tags

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum