Posted on 1999-12-04
Medium Priority
Last Modified: 2010-05-02
Give me Standard Step By Step Programming Method (Logic) TO
call API From VB.
Question by:nkhusain
LVL 14

Accepted Solution

mcrider earned 20 total points
ID: 2254775
Calling an API from VB is pretty easy.  There is an "API Text Viewer" that comes with VB.  All of the windows APIs defined can be copied to the clipboard from it and pasted into your program from it.

When you run it, open the file "Win32api.txt".  This file has all of the definitions in it.

To call an API you need to put a DEFINE statement in your program... Either in the DECLARATION SECTION of a form or in a MODULE.

If you use a form, you must make it a PRIVATE DECLARATION.

An example of an API is the GetCursorPos API.  With it, you can get the position of the mouse pointer on the screen.

For example, put the following in a module:

        x As Long
        y As Long
End Type
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Notice that the function uses a type of POINTAPI and the type is defined above.  This type declaration is also found in the API viewer... After you load the "Win32api.txt" file into the viewer, just select "types" in the dropdown box...

Now to use the GetCursorPos function:

Put the following code in a timer event on a form and run the program:

Dim MouseLoc as POINTAPI
dim Retcode as Long
debug.print MouseLoc.x, MouseLoc.y

the mouse's physical X/Y location will be displayed in the Immediate Window...


Now whats's happening...

The GetCursorPos takes the argument MouseLoc and loads the X/Y items of it and when the API terminates, Retcode is loaded with the return value of the API.

Simple huh?


Expert Comment

ID: 2256858
'more the merrier..here's another.
'it's Christmas

'Move a file using API
'put this in the gen declarations

Private Declare Function MoveFile Lib "kernel32.dll" Alias _
"MoveFileA" (ByVal lpExistingFileName As String, ByVal _
lpNewFileName As String) As Long
'put this in the command button

' The original file will no longer exist.  Note how this example both changes
' the filename and moves the file into a different directory simultaneously.

Dim retval As Long  ' return value

retval = MoveFile("C:\download\file.txt", "c:\my documents\file.txt")


Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question