Link to home
Avatar of Bryce Bassett
Bryce BassettFlag for United States of America

asked on

VBA declare statements for Windows API calls showing red font (error)

I have some Windows API Declaration statements at the top of my VBA code module.  I've used the approach shown below to make them work for both 32 and 64 bit systems.  But since I upgraded my computer recently (using a 64-bit desktop), I notice that the 32 bit declare statements are now giving an error (showing up in red font in my code editor).  And anywhere in the code where I call the APIs is now giving me a compile error as well.

Anybody recognize what is going on and know how to fix this?

Thanks!
User generated image
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Try changing the first #if to

#If Win64 Or VB7 Then
Hard to tell from an image, but did you copy that lines? Maybe some hidden chars?
As an aside, it's better to paste your code into a post, select the text and then click the "Style Code" button. That way we can copy your code and use it for our testing.

The 64 bit operating system really has nothing to do with VBA, the issue is which version of Office you are using.

I believe Martin's changes should resolve the issue.  What is strange is that MS decided to use a variable named Win64 rather than Off64 to determine which version of Office you are running.



Avatar of Bill Prew
Bill Prew

It's normal in those conditional compile sections for one section (the one that doesn't apply hopefully) to show up in red.  That won't be a problem though since those lines will never be compiled or executed due to the conditional compile directives.


Are you running this code in a 64-bit Office installation?

These are my two go to docs for 32 versus 64 Office and VBA.  I do often see folks just use VBA7 as the check for 64 bit though interestingly...



»bp
Just because you see red font doesn't mean that you have error.
Just compile your code...its normal for dual bitness declarations..
Just compile your code...its normal for dual bitness declarations..
I can only speak to 32-bit environments and there that's not true.

I agree with John that the declarations which are not valid for your current environment will normally display as red, but as John said, those lines should not be compiled if you are using the correct conditional compilation tests (thus Win64 OR VBA7) that Martin mentioned.

Here's a simple #If/#Else. For those of you who use 64-bit does the second declaration show up as red?
Private WithEvents mcTree As clsTreeView
#If Win64 Or VB7 Then
    Private Declare PtrSafe Function LockWindowUpdate Lib "user32" (ByVal hWndLock As LongPtr) As Long
#Else
    Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwnd As Long) As Long
#End If

Open in new window

Here is a view from one of my applications with dual personality (32/64)
User generated image
That doesn't show your #If. Is it the same as mine?
#If VBA7 Then

Open in new window

I use
If you can, change it to #If Win64 Or VB7 and see what happens, or just paste my code from here into a new workbook.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Bryce Bassett

ASKER

Thanks everyone for chiming in!   I'll dig into these responses over the weekend,
Changing #If VBA7 to #If Win64 Or VBA7 does not make any difference, as Bill suggested.  Text remains red, but project still compiles OK.  

Thanks all for your help!