We help IT Professionals succeed at work.

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

Bryce Bassett
Bryce Bassett asked
on
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!
Declare statements
Comment
Watch Question

Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Try changing the first #if to

#If Win64 Or VB7 Then
ste5anSenior Developer

Commented:
Hard to tell from an image, but did you copy that lines? Maybe some hidden chars?
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:

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.



Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:

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?

Bill PrewTest your restores, not your backups...
Top Expert 2016

Commented:
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
John TsioumprisSoftware & Systems Engineer

Commented:
Just because you see red font doesn't mean that you have error.
Just compile your code...its normal for dual bitness declarations..
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Just compile your code...its normal for dual bitness declarations..
I can only speak to 32-bit environments and there that's not true.
Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010

Commented:

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.

Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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

John TsioumprisSoftware & Systems Engineer

Commented:
Here is a view from one of my applications with dual personality (32/64)
Clipboard01.jpg
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
That doesn't show your #If. Is it the same as mine?
John TsioumprisSoftware & Systems Engineer

Commented:
#If VBA7 Then

Open in new window

I use
Martin Liss"There is still no cure for the common birthday." ~John Glenn
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.
Test your restores, not your backups...
Top Expert 2016
Commented:
VBA7 is enough to check for the API definitions.  It is merely to detect VBA version 7, which added support for LongPtr and PtrSafe.  So VBA7 is used to pick the API definitions with or without those, based on VBA version.

Yes, Win64 will tell you if you are on a 64 bit version of Offic, but this isn't needed for the API declaration.  VBA& is smart enough once you adjust the APIs to use LongPtr to compile that based on the version of Office, 32 or 64 bit.  So it does it "magically" behind the scenes, no need to clutter the code with redundant checking.

So the code the author originally posted in the question is the typically approach, and then the compiler handles the rest from there.
Bryce BassettFreelance VBA programmer

Author

Commented:
Thanks everyone for chiming in!   I'll dig into these responses over the weekend,
Bryce BassettFreelance VBA programmer

Author

Commented:
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!