Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

API declarations for Win32 on a 64 bit machine

Status
Not open for further replies.

MattGreer

Technical User
Feb 19, 2004
471
US
So I've run across this issue before and wasn't sure if there was a workaround.

In this example on stackoverflow, (scroll to the end of the comments for the example code, there's an IF statement in the API declarations section that checks for 64 vs 32 bit. On a 64 bit machine, all the code after the "#Else" statement is an error; it's all red. Code hasn't even run yet, Excel just recognizes that there's an issue with it.

Is there a workaround? I do have a 32 bit machine as well and I'd really like to be able to use this spreadsheet on either machine.

Oh, I'll paste the code here if that helps:

Code:
' API declarations
#If VBA7 And Win64 Then
    Private Declare PtrSafe Function GetVersionEx Lib "Kernel32" _
        Alias "GetVersionExA" _
        (lpVersionInformation As OSVERSIONINFO) As Long

    Private Declare PtrSafe Sub keybd_event Lib "user32" _
        (ByVal bVk As Byte, _
        ByVal bScan As Byte, _
        ByVal dwflags As Long, ByVal dwExtraInfo As Long)

    Private Declare PtrSafe Function GetKeyboardState Lib "user32" _
        (pbKeyState As Byte) As Long

    Private Declare PtrSafe Function SetKeyboardState Lib "user32" _
        (lppbKeyState As Byte) As Long
#Else
    Private Declare Function GetVersionEx Lib "Kernel32" _
        Alias "GetVersionExA" _
        (lpVersionInformation As OSVERSIONINFO) As Long

    Private Declare Sub keybd_event Lib "user32" _
        (ByVal bVk As Byte, _
        ByVal bScan As Byte, _
        ByVal dwflags As Long, ByVal dwExtraInfo As Long)

    Private Declare Function GetKeyboardState Lib "user32" _
        (pbKeyState As Byte) As Long

    Private Declare Function SetKeyboardState Lib "user32" _
        (lppbKeyState As Byte) As Long
#End If

Thank you for your help!



Thanks!!


Matt
 
Hi Matt

I appreciate that some time has passed since your posts, but perhaps for the benefit of anyone else who comes across this post, it is worth noting that the #If/#Else construct you referenced is conditional compilation, and depending on the constants used, will direct VBA through either of the two routes. For those using 64 bit office, all API Declarations require the addition of the word PtrSafe. If they don't, then they will appear in red as though there is an error in the line of the code. If the API Declarations in red appear in the 32-bit part of the conditional compilation (ordinarily, the bottom half) then that will mean that you're viewing the code on a 64bit version of Office, and provided that the conditional compilation is set up - you can ignore the red lines in 32bit section because VBA certainly will be.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top