ACC: Start Files or Hyperlinks with Windows API ShellExecute()
ID: Q148632
|
The information in this article applies to:
-
Microsoft Access versions 7.0, 97
SUMMARY
Advanced: Requires expert coding, interoperability, and multi-user skills.
This article shows you how to use the Windows 32-bit application
programming interface (API) ShellExecute() function to start an application
associated with a given file extension without having to know the name of
the associated application. For example, you can start Microsoft Paint by
passing the file name Bubbles.bmp to the ShellExecute() function. Or, you
can connect to the World Wide Web (by using a Web browser installed on your
computer) by passing a hyperlink or URL (Uniform Resource Locator) to the
API function.
NOTE: Microsoft Access 97 has this functionality built in.
This article assumes that you are familiar with Visual Basic for
Applications and with creating Microsoft Access applications using the
programming tools provided with Microsoft Access. For more information
about Visual Basic for Applications, please refer to the "Building
Applications with Microsoft Access for Windows 95" manual.
MORE INFORMATION
To use the Windows API ShellExecute(), you must first declare the function in a standard Visual Basic for Applications module. After you have declared the function, you can use the function by following one of the two
examples described later in this article.
Declaring the Windows API ShellExecute()
- Open the sample database Northwind.mdb and create a new module named
Module1.
- Add the following code to the Declarations section:
Option Explicit
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
Global Const SW_SHOWNORMAL = 1
- Close and save Module1.
To test the ShellExecute() function, use one of the following examples.
Example 1: How to Connect to the World Wide Web
NOTE: This functionality is built in to Microsoft Access 97.
- Create a new table with the following structure:
Table: WebSites
-------------------------------
FieldName: SiteID
DataType : AutoNumber
Indexed: Yes (No Duplicates)
FieldName: SiteURL
DataType : Text
- Save the table as WebSites and switch the table to Datasheet view. Enter
the following three records:
SiteID SiteURL
----------------------------------------
1 ftp.microsoft.com
2 www.microsoft.com/kb.htm
3 http://www.microsoft.com/devonly
- Use the AutoForm: Columnar Wizard to create a new form based on the
WebSites table.
- Switch the form to Design view and add the following command button:
Command button:
Name: cmdConnect
Caption: Connect to Web
- Set the cmdConnect button's OnClick property to the following event
procedure:
Private Sub cmdConnect_Click()
On Error GoTo cmdConnect_Click_Error
Dim StartDoc As Long
If Not IsNull(Me!SiteURL) Then
StartDoc = ShellExecute(Me.Hwnd, "open", Me!SiteURL, _
"", "C:\", SW_SHOWNORMAL)
End If
Exit Sub
cmdConnect_Click_Error:
MsgBox "Error: " & Err & " " & Error
Exit Sub
End Sub
- Switch the form to Form view.
- Click the Connect To Web button. Note that your Web browser is started
automatically and displays the Web site for the URL listed in the
current record.
Example 2: How to Open a File in Its Associated Application
- Open Module1 and create the following procedure:
Function StartDoc (DocName As String)
On Error GoTo StartDoc_Error
StartDoc = ShellExecute(Application.hWndAccessApp, "Open", DocName,
_ "", "C:\", SW_SHOWNORMAL)
Exit Function
StartDoc_Error:
MsgBox "Error: " & Err & " " & Error
Exit Function
End Function
- On the View menu, click Debug Window.
- In the Debug window, type the following line, and then press ENTER:
StartDoc "Bubbles.bmp"
Note that the function starts Microsoft Paint, which loads the
Bubbles.bmp file.
NOTES
- The Windows API ShellExecute() function differs from the Visual Basic
Shell() function in that you can pass the ShellExecute() function the
name of a document, and it will start the associated application, and
then pass the file name to the application. You can also specify the
working folder for the application.
- The return value for the StartDoc() function is the same as for the
Shell() function. It is the Windows instance value of the application
that was started.
REFERENCES
For information about using the Windows 16-bit API ShellExecute() function in Microsoft Access version 2.0, please see the following article in the Microsoft Knowledge Base:
Q121157 How to Start Doc with Windows API ShellExecute() Function
For more information about the ShellExecute() function, please see the
Microsoft Win32 SDK "Programmer's Reference."
Additional query words:
Keywords : kbprg
Version : WINDOWS:7.0,97
Platform : WINDOWS
Issue type : kbhowto
|