top of page

Automatically add Windows API declaration(s)

  • Writer: John
    John
  • Aug 18
  • 6 min read

Updated: Oct 30

VBE_Extras will help you to identify and add (to your VBA code) Windows API declarations … as of this functionality first being added to VBE_Extras in August 2025, VBE_Extras is aware of the VBA declarations for:


  • 1,749 Functions (counting Functions that include both an ANSI and Wide variant as 1 … if counted as 2 discrete Functions then that would add several hundred more)

  • 466 Types

  • 19 Enums

  • 9,328 Consts

     

This functionality is accessed via the VBE menu: Extras > Other > Add Windows API declarations (or, as with other VBE_Extras functionality, you can create a keyboard shortcut or add it to the VBE_Extras Toolbar) which shows the 'Windows API declarations' dialog in which, you can:


  • View a list of all Windows API declarations that VBE_Extras is aware of – the list includes:

    • The type and name of the declaration

    • It's 'bitness' (some declarations are 32 or 64 bit only; the majority are 'both')

    • If the declaration is ANSI-specific, whether a Wide (i.e. Unicode) variant is also available

    • The number of implementation example(s) available, if any (see Examples)

    • The full code of the declaration - the text in the 'Code' column is fixed (for example, the 'Code' column always shows the ANSI version of the declaration), however, the tooltip for this column is updated dynamically based on other selections made in this dialog and also in the VBE_Extras Settings

  • Search / filter for Windows API declarations – any search text that you enter:

    • Always searches by declaration name

    • Always searches the description of any implementation example for the Windows API declaration

    • For Functions, Types and Enums (not Consts), the (English-language) text description for the declaration is searched … for example, a search for "class*name" will display 3 Functions: FindWindowA, FindWindowExA and GetClassNameA (the words "class" and "name" are included in the description of both FindWindowA and FindWindowExA; those two words are included in the name of GetClassNameA)

  • Filter by Windows API declaration type – that is, show all declarations or only show Functions, Types, Enums or Consts

 

Any combination of Windows API declaration(s) can then be selected. If:


  • You select a 32 bit declaration, you will be asked whether you want the 64 bit equivalent to also be selected (and vice-versa)

  • You select a Function that is dependent on a Type or an Enum (e.g. a Function that take a Type as a parameter) then you will be asked if you want the Type to also be selected

    • The same also happens for Types that are dependent on other Types; Types that are dependent on Consts; and Consts that are dependent on other Consts (this is recursive so that, for example, if you select a Function that depends on a Type that is itself dependent on another Type that is dependent on a Const, you will be asked if you want all of those dependencies to be selected)

  • You select a Const that is part of a related group of Consts e.g. if you select the ABE_RIGHT Const, you will be asked if you want the 3 other related Consts (ABE_BOTTOM, ABE_LEFT and ABE_TOP) to also be selected

 

You can then choose various options for the format of the Windows API declarations. Options exist for:


  • Adding only a VBA7 declaration or both a VBA7 and a pre-VBA7 (i.e. VBA6) declaration using appropriate conditional compilation

  • Adding the ANSI or Wide (i.e. Unicode) variant of any Functions that have both variants … and, if Wide, whether to convert the value type of any String parameters (and return value) to Long (for pre-VBA7) or LongPtr (for VBA7) … the reason for doing this is to allow pointers to the Strings (using StrPtr) to be passed to the Windows API Function instead of the String itself (which then avoids VBA's automatic conversion of all Strings into ANSI when passing them to Windows API functions … even when that function is a Wide / Unicode Function … for examples of using StrPtr to pass a pointer instead of the actual String, see MsgBox with a timeout or MsgBox with custom button texts

  • Adding the declaration(s) as Private, Public or 'default'

  • If and how to split the line for Function declarations (ie that are very long)

 

You can then add the selected Windows API declaration(s) to the active Module:


  • As a discrete block of code with conditional compilation directives for VBA7 and / or Win64 added where required (this option is always available), or

  • Merged with any existing Windows API declarations already in the Module … that is, new declarations will be added within the appropriate existing conditional compilation directives for VBA7 and / or Win64 (this option is only available when VBE_Extras detects existing Windows API declarations in the active Module and when the directives can be correctly parsed by VBE_Extras)

  • Alternatively, if you would rather add the Windows API declaration(s) yourself (or, otherwise you just want the text of the declaration(s)), you can use the 'Text' button

 

VBE_Extras also stores details of which Windows API declaration(s) you have selected and allows you to quickly add them again using the 'Recent' button.

 

There is a right-click context menu for the list of Windows API declarations … depending on which declaration is clicked, there are options to:


  • Open the web page for the declaration (the web page will always be the English language web page)

  • Copy the URL of the web page for the declaration (the URL will always be the English language URL)

  • Copy the description of the declaration

  • Copy the declaration (either the VBA7 version, the pre-VBA7 version, or both)

  • View implementation example(s) of the declaration ... see Examples

 

For more information on this functionality, please see the 'Add Windows API declaration(s)' section of the VBE_Extras user guide.


Examples


As of this functionality first being added to VBE_Extras in August 2025, VBE_Extras is aware of examples for 239 Functions plus examples for a number of Types, Enums and Consts (i.e. so not all declarations that VBE_Extras is aware of have examples ... but the bulk of examples tend to be for the most commonly used declarations).


All examples are online (you will need to be connected to the internet to view examples) on VBA-related sites ... either Q&A web sites (such as StackOverflow, MrExcel) or blog posts. Other than those examples that are on this website, I have no direct control over the content (which may change over time or even be removed) so please use caution when implementing code.


Example usages are intended to assist with implementation of the Windows API declaration in your code. The example(s) for a particular declaration may or may not relate to how you want to use the declaration … but should give some hint as to its usage which is often not obvious from the 'bare bones' declaration and it's Microsoft web page alone. As all examples are online, then it would be entirely possible for you to find the examples yourself (and other examples may well be available online, especially for the more regularly used Windows API declarations … please don't consider the example(s) linked from VBE_Extras to be definitive) … but this is intended to allow you to quickly find an example to assist with implementation in your own code.


I will publish a number of Windows API-related blog posts over the coming weeks and months to add examples for:

  • Some Windows API Functions that don't have an example, and

  • Some Windows API Functions that do already have an example but that example covers a large number of Windows API Functions and so the code is not as clear as it might be (i.e. not as clear as a specific / targeted example)


Before running code that uses Windows API declaration(s) …


Note that you should always make a backup of your VBA project (see Restore points for VBA code) before running code that uses Windows API declaration(s) … whether those declarations were added using VBE_Extras or not. Using Windows API declarations incorrectly can result in an immediate crash of the host application and data loss.

 

I have not personally tested each of the declarations that VBE_Extras can add to your VBA code (I have tested many of them … but not all of them!) and so I cannot provide a guarantee that every declaration is correct. A large number of the declarations that VBE_Extras can add come from Microsoft's "Win32API_PtrSafe.TXT" file … this file includes many errors (which are fixed within VBE_Extras … for those errors that I am aware of) and includes a number of deprecated declarations (which are excluded from those that VBE_Extras will add … as far as I know them).


Additionally, even if a declaration is correct, passing it invalid data or otherwise using it incorrectly can also result in an immediate crash of the host application.


Update October 2025


As of release 1.7.1.2, VBE_Extras is aware of the VBA declarations for:


  • 1,983 Functions (counting Functions that include both an ANSI and Wide variant as 1)

  • 482 Types

  • 45 Enums

  • 10,701 Consts


Examples are now split in that there are 90 examples contained entirely within VBE_Extras in addition to VBE_Extras being aware of 116 examples on websites). Those examples cover 432 Functions plus a number of Types, Enums and Consts.

Comments


bottom of page