How to Fix the VBA Expected: Lib Compile Error After Adding PtrSafe
Question details
The user is encountering an "Expected: Lib" compile error in VBA while attempting to make an API declaration compatible with 64-bit environments.

- Product
- VBA Macros
- Device & OS
- not provided
- Scenario
- Updating an older 32-bit macro by adding the PtrSafe keyword to the HtmlHelpA API declaration, which unexpectedly triggers a compilation error.
- Observed behavior
- The VBA compiler throws an "Expected: Lib" error because the PtrSafe keyword is duplicated or placed in the wrong position within the function declaration.
Verify whether you are running a 32-bit or 64-bit version of your spreadsheet application, as the PtrSafe keyword is strictly required for 64-bit environments but must follow precise syntax rules.
Correct the Syntax and Placement of the PtrSafe Keyword
Resolve the compile error by ensuring PtrSafe is used exactly once and placed immediately after the Declare keyword.
The "Expected: Lib" error typically occurs when the VBA compiler encounters unexpected keywords before the Lib specification. For 64-bit API declarations, the PtrSafe attribute must follow a strict structural order: Declare PtrSafe Function [Name] Lib [LibraryName]. Using it twice or placing it after the function name will break the compilation.
Press Alt + F11 on your keyboard to launch the Visual Basic for Applications editor.
Find the module containing your Windows API declarations, specifically the line attempting to declare the HtmlHelp function from hhctrl.ocx.
Inspect the line for multiple instances of 'PtrSafe'. Delete any extra PtrSafe keywords so that only one remains.
Adjust the statement so PtrSafe sits directly between 'Declare' and 'Function' (or 'Sub'). The correct syntax should look exactly like this: Declare PtrSafe Function HtmlHelp Lib "hhctrl.ocx" Alias "HtmlHelpA" (ByVal hwndCaller As LongPtr, ByVal pszFile As String, ByVal uCommand As LongPtr, ByVal dwData As Long) As Long.

Edit and Run 64-bit VBA Macros Seamlessly in WPS Office
WPS Office provides robust built-in support for VBA macros. You can easily modify your API declarations, resolve compile errors, and run complex scripts directly within the WPS Spreadsheet Developer environment.
- 1. Open Your Macro Workbook: Launch WPS Office and open your macro-enabled spreadsheet.
- 2. Access Developer Tools: Navigate to the Developer tab on the top ribbon menu.
- 3. Launch the VBA Editor: Click on 'Visual Basic' or press Alt+F11 to open the built-in VBA editor.
- 4. Update and Run: Locate your API declaration, correct the PtrSafe placement, save the module, and run your macro effortlessly.

Frequently Asked Questions
What does the 'Expected: Lib' error mean in VBA?
This compile error occurs when VBA encounters an improperly formatted Windows API declaration. It usually indicates that the 'Lib' keyword is missing, placed in the wrong order, or preceded by invalid syntax such as a duplicated PtrSafe keyword.
Why do I need to use the PtrSafe keyword?
The PtrSafe keyword is required in 64-bit versions of Office applications to safely declare Windows API functions. It explicitly informs the VBA compiler that the Declare statement has been verified and is designed to run in 64-bit memory environments.
What is the difference between Long and LongPtr in 64-bit VBA?
Long is a fixed 32-bit data type, whereas LongPtr is a dynamic data type that resolves to 32 bits on 32-bit systems and 64 bits on 64-bit systems. You must use LongPtr for handles and memory pointers when adapting APIs for 64-bit environments.
How can I make my VBA code work on both 32-bit and 64-bit systems?
You can use conditional compilation with '#If VBA7 Then' to provide a 64-bit declaration (using PtrSafe and LongPtr) alongside a fallback 32-bit declaration for older versions. This ensures your macro runs seamlessly across different system architectures.




