Open/Print Files, View Web Sites,
Connect to the Internet, Pre-Fill E-Mail Fields...
See how to start the application associated with a given file and tell it to
open that file. Say you have the path of a Microsoft Word document, an Excel
spreadsheet, a bitmap file, or an html file, etc. and you want to open it from
your program. You can call ShellExecute. (To
create a desktop shortcut to do the same, see my Create
Shortcuts w/ Windows Scripting Host page).
ShellExecute lets you open or print a file
using the default program associated with that file. It finds the
associated application for you. Plus, files need not be "real"
files. You can pass a URL such as "www.TheScarms.com" and it will start
your default browser and open the specified Web site (provided you have an
internet connection. I will get to that in a moment). You can even
use it to Explore the contents of a folder. See the examples
If you need to know the application associated with a file you can use the
FindExecutable function. You call FindExecutable
with the name of the file. It returns the path of the associated
application. In the code below I create a small .htm file.
FindExecutable returns the path of the associated program - my
default browser. Then I use ShellExecute to
open my browser and view the web site that was entered into a textbox.
I used FindExecutable to get the program associated
with an .htm file. You can do the same with any registered file
extension. Obviously you probably will not need to create a file as I am
doing here. All you have to do is point to a file with the extension of
A complete discussion of the FindExecutable and
ShellExecute functions are provided in the source code.
Okay, back to the internet connection...
If you are using Dial Up Networking (DUN) to
connect to the internet, you can have your program automatically dial your
default DUN connection and hang-up when you are done. The
InternetAutodial and InternetAutodialHangup
functions of Wininet.dll make this
possible. This DLL comes with IE3+ or is available from Microsoft's
site. If you do not have this DLL you can also start your DUN connection
as shown in my Start Control Panel Applets with Rundll32
Web Enable Your Apps
Let users browse the web inside your app. You can add the Microsoft
Internet Control but the user must have Internet Explorer installed
otherwise the app will fail to start. In this sample I load it dynamically to
avoid the problem. Enter a URL in the textbox and click the "Display a Web
Page on Your Form" button. Once the page loads, you can resize
Download Source Code
In order for ShellExecute to work Windows must
already have associated an application with your file type. For instance,
if you double click a file in Explorer and you get the Open With dialog,
there is no application associated with that file type and ShellExecute
To open Excel and view a particular spreadsheet:
Call ShellExecute(Me.hwnd, "open", "TheScarms.xls", "", 0, SW_SHOWNORMAL)
To tell Excel to print the spreadsheet instead:
Call ShellExecute(Me.hwnd, "print", "TheScarms.xls", "", 0, SW_SHOWNORMAL)
To Explore the contents of a folder:
Call ShellExecute(Me.hwnd, "explore", App.Path, "", 0, SW_SHOWNORMAL)
To start your default browser and view a particular Web site:
Call ShellExecute(Me.hwnd, "open", "www.TheScarms.com", "", 0, SW_SHOWNORMAL)
Copy the following code to the Declarations section of a form:
Const SW_SHOWNORMAL = 1
Const Internet_Autodial_Force_Unattended As Long = 2
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 Long
Private Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" _
(ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
Private Declare Function InternetAutodial Lib "wininet.dll" _
(ByVal dwFlags As Long, ByVal dwReserved As Long) As Long
Private Declare Function InternetAutodialHangup Lib "wininet.dll" _
(ByVal dwReserved As Long) As Long
|Code for FindExecutable and Connecting/Disconnecting to/from the Internet
To find an associated program yourself and run it:
Private Sub cmdGo_Click()
Dim sFileName As String
Dim sDummy As String
Dim sBrowserExec As String * 255
Dim lRetVal As Long
Dim iFileNumber As Integer
' Create a temporary HTM file
sBrowserExec = Space(255)
sFileName = "C:\TheScarms.HTM"
iFileNumber = FreeFile
Open sFileName For Output As #iFileNumber
Write #iFileNumber, "<HTML> <\HTML>"
' Find the default browser associated with the .htm file.
lRetVal = FindExecutable(sFileName, "", sBrowserExec)
sBrowserExec = Trim$(sBrowserExec)
' If a browser was found, launch it.
If lRetVal <= 32 Or IsEmpty(sBrowserExec) Then
MsgBox "Could not locate your Browser", _
vbExclamation, "Browser Not Found"
lRetVal = ShellExecute(Me.hwnd, quot;open", sBrowserExec, _
txtUrl.Text, "", SW_SHOWNORMAL)
If lRetVal <= 32 Then
MsgBox "Web Page Not Opened", _
vbExclamation, "URL Failed"
To connect to the Internet:
Private Sub cmdConnect_Click()
Dim lResult As Long
lResult = InternetAutodial(Internet_Autodial_Force_Unattended, 0&)
To Disconnect from the Internet:
Private Sub cmdConnect_Click()
Dim lResult As Long
lResult = InternetAutodialHangup(0&)
Run the code and click Connect to Internet to connect to the Internet
using your default Dial Up Networking connection. Once connected enter a
URL into the textbox such as "www.TheScarms.com" and click the Visit Web Site
button to open your default borwser. To display the page on your form,
click the Display a Web Page on Your Form button. When done, click Disconnect
from Internet to terminate your connection.
It is most informative if you step through the code. The Visit Web Site button
on the ShellExecute frame assumes you already have an internet connection