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
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.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:
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 Open button See Figure 3
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.
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
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
errorHandler:
MsgBox Err.Description, vbCritical, "Error"
End Sub
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 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.
Please rate the tutorial
Learn SQL Programming By Examples [Kindle Edition]2.99
Learn PHP Programming by Examples [Kindle Edition] $2.99
Learn Visual Basic 6.0 [Kindle Edition] $1.99
How to Build Your Own Web Site from Scratch [Kindle Edition] $1.49
New-trip.com website source code
| Comments | |
|---|---|
- Home
- vb_api
- Batch Files
- Java Properties
- Form Validation
- Display Image PHP
- Upload File PHP
- phpMyAdmin
- Environment Variables
- Delete Trojan horse
- VB Code Examples
- Learn Visual Basic
- VB Form Events
- VB Password Keeper
- Deployment VB App
- Using API in VB
- Database Design
- Password Keeper
- MySQL and Excel
- Learn MySQL
- Learn PHP
- Learn C++
- Learn Java
- Web Site Tips
- User_Auth. Demo
- Set Shopping Cart
- Using Twitter
- Advertise here
- Site map
- Registration
Web programming Tips