Computer Programming web Web programming Tips

How to use Windows API function in Visual Basic 6.0 - VB code example.

By Sergey Skudaev

In the following VB code example I show you how to use the ShellExecute Windows Application Interface (API) function in VB. Before study this tutorial you have to be familiar with start VB (Visual Basic) tutorial.

Start Visual Studio and select Visual Basic 6.0. Select a new standard project. The default VB form displays. In properties window change form name to frmAPI .In the form caption property type "Windows API function example".

Click command button on toolbox and draw it on the form. Select the command button name property and change to cmdOpen. Select the command button caption property and change to Open. Draw one more button and change its name to cmdPrint. Change its caption to Print.

Select Project from main menu and click Add Module. Hover mouse over a thumbnail to see a large image. See picture 1

VB API tutorial: Add module

standard module

Figure 1: Add Standard Module

Select property window and change module name to WinMain. Open WinMain code window and click Tools, Add Procedure from main menu. See figure 2.

VB API tutorial: Add Procedure
Sub Main

Figure 2: Add Sub Main Procedure

Enter Main in the procedure name and click OK button. Inside Main () procedure type a code to display frmAPI form:


Select Start, Microsoft Visual Studio 6.0, Microsoft Visual Studio 6.0 Tools, API Text Viewer.

Select File, Load File and select WIN32API.txt in open file windows. Click Open button See Figure 3

VB API tutorial: API Text Viewer

VB API tutorial: WIN32API.txt

Figure 3. API Text Viewer and Window API text file.

The list of API functions displays. Type "Shellexecute" in search text field and select the ShellExecute function in the list. Click the Add button. The function declaration displays in the lower text area.

Public Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As String) As Long

Select it and press CTRL + C to copy. Open WINMAIN code window and paste the API function declaration under Option Explicit.

Copy the Public Sub ShellOpenFile(sPath As String) procedure code in WINMAIN code window.

VB API tutorial: function declaration

WINMAIN.bas source code:

Option Explicit

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

   Public Sub MAIN()
   End Sub

   Public Sub ShellOpenFile(sPath As String)
       On Error GoTo errhandler

      Dim sDefaultDirectory As String

'Call API function

      Call ShellExecute(0, "open", sPath, "", sDefaultDirectory, 1)
      sPath = ""
      Exit Sub
      MsgBox Err.Description
      Resume exitShel
   End Sub

Click Project from the main menu and select Reference. The Reference window displays. Scroll down to Microsoft Word 11.0 Object Library. We need this library to use MS Word Application objects. See figure 4

VB API tutorial: function declaration

Figure 4. References window.

Click Project from the main menu and select Project Properties. Enter project name APIexample. Select Sub Main in the Startup Object combobox.

Click OK button.

Open frmAPI form code window and enter source code inside
Private Sub cmdOpen_Click() procedure and inside
the Private Sub cmdPrint_Click() procedure.

frmAPI.frm source code:

   'Declaration of the Word Application object

   Public app As New Word.Application

   Option Explicit

   Private Sub cmdOpen_Click()

      Dim sPath As String

   On Error GoTo errorHandler

      'Set word app object

      Set app = New Word.Application

      sPath = "C:\Program Files\Microsoft Office\OFFICE11\WINWORD.EXE"

      ShellOpenFile sPath

   Exit Sub


      MsgBox Err.Description, vbCritical, "Error"

   End Sub

   Private Sub cmdPrint_Click()

   On Error GoTo errorHandler

      'Activate word application


      'If Active Document opened

      If Not (app.ActiveDocument Is Nothing) Then

      'assign contents to the active document

      app.ActiveDocument.content = "Hello World!"

      'print active document


      End If

   Exit Sub


      MsgBox Err.Description, vbCritical, "Error"

   End Sub

Save frmAPI.frm and WINMAIN.bas and APIexample.vbp files in the new APIexample folder.

Run the project. The frmAPI form displays.

Click Open button. It will call ShellExecute API function. MS Word Application starts and a Word document opens.

Make sure the printer is on. Click Print button. "Hello World!" will be displayed on the open Word page and pinter will print the page.

My eBooks on


No comments yet.

Add Comment

* Required information
(never displayed)
Smile Sad Huh Laugh Mad Tongue Crying Grin Wink Scared Cool Sleep Blush Unsure Shocked
Type the word for the number 9.
Enter answer:
Enter code:
Notify me of new comments via email.
Remember my form inputs on this computer.
I have read and understand the privacy policy. *
I have read and agree to the terms and conditions. *
Powered by Commentics