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 the Visual Studio and select Visual Basic 6.0. Select a new standard project. The default VB form displays. In the properties window, change form name to from API. In the form caption property type "Windows API function example.".

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

Select Project from the 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 the property window and change module name to WinMain. Open WinMain code window and click Tools, Add Procedure from the 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 the frmAPI form:

frmAPI.Show

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 the 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 is displayed. Type "Shellexecute" in the search text field and select the ShellExecute function in the list. Click the Add button. The function declaration is shown 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 the 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()
   frmAPI.Show
   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 = ""
   exitShel:
      Exit Sub
   errhandler:
      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 combo box.

Click OK button.

Open frmAPI form the code window and enter the source code inside
the 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

   errorHandler:

      MsgBox Err.Description, vbCritical, "Error"

   End Sub

Subscribe to our code examples

responsive website

The complete source code for a responsive website with user registration and authentication.

Dowload for free

   Private Sub cmdPrint_Click()

   On Error GoTo errorHandler

      'Activate word application

      app.Activate

      '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

      app.ActiveDocument.PrintOut

      End If

   Exit Sub

   errorHandler:

      MsgBox Err.Description, vbCritical, "Error"

   End Sub

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

Run the project. The frmAPI form is displayed.

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

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

My eBooks on Amazon.com

US    UK    BR    CA
US   UK   BR   CA
US    UK    BR    CA