Avatar of Bryce Bassett
Bryce Bassett
Flag 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!
Declare statements
Microsoft OfficeWindows OSVBA

Avatar of undefined
Last Comment
Bryce Bassett

8/22/2022 - Mon
Martin Liss

Try changing the first #if to

#If Win64 Or VB7 Then
ste5an

Hard to tell from an image, but did you copy that lines? Maybe some hidden chars?
Martin Liss

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Dale Fye

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 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?

Bill Prew

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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
John Tsioumpris

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

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 Fye

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.

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Martin Liss

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 Tsioumpris

Here is a view from one of my applications with dual personality (32/64)
Clipboard01.jpg
Martin Liss

That doesn't show your #If. Is it the same as mine?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
John Tsioumpris

#If VBA7 Then

Open in new window

I use
Martin Liss

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
Bill Prew

THIS SOLUTION 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
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Bryce Bassett

ASKER
Thanks everyone for chiming in!   I'll dig into these responses over the weekend,
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Bryce Bassett

ASKER
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!