Compile error when attempt to run macro with #If VBA7 = True Then...

steve_n

New Member
Joined
Apr 28, 2019
Messages
5
Hello Excel gurus,
I am new to this forum, this is my first posting, so my apologies in advance if I miss a posting rule...
Doing Excel VBA engineering automation just for fun, I am not a professional programmer.
Have a small code that calls a DOS program (spice.exe) through the Shell. It worked well with 32-bit Excels (in Windows7). I modified it to run in 64-bit Excel, by using Ptrsafe and LongLong, it works fine (on Windows10 and 64bit Excel). However, when I try to make a code that detects the version of Excel and use the lines starting with #If VBA7 = True ..., I get an error. I understand that the 64-bit Excel version will report the legacy part of code as an error and I should ignore it, but the 64bit Excel simply refuses to execute the macro (have not had a chance to try with 32bit Excel yet). Again, if I remove the lines involving the conditional compilation, and just include the function declaration for the corresponding Excel version, the code works OK either in 32bit or in 64bit Excels. I assume I am missing something very basic about conditional compilation. Below is the part of the code that gives me trouble. Can anyone give me a hint?
Thanks

Code:
Option Explicit
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 = True Then
    ' 64 Bit API
    Private Declare PtrSafe Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPtr
    Private Declare PtrSafe Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As LongPtr, lpExitCode As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
    ' 32 bit API
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If
[/end code]
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Thanks for the suggestion. Unfortunately with #IF Win64 = True Then... the result is the same. When I run the macro, it stops with the error message highlighting the 32-bit portion of the conditional code and refuses to continue. Maybe there is a switch somewhere in Excel where I have to tell the application to ignore the warning?
 
Upvote 0
What exactly is the error message you're getting?

Is your VBAProject perhaps missing a reference to the Object Library?
 
Upvote 0
The error message is a popup window over the VBA code with the 32-bit declaration lines highlighted in red. The popup window says "Compile error: The code in this project must be updated for use on 64-bit systems. Please review and update Declare statements and then mark them with the PtrSafe attribute". This is the same error message I get when I just try to run the old 32-bit code on 64-bit Excel. And the code runs OK on 64-bit Excel if I remove the conditional declaration and just include the 64-bit declaration lines. Seems as if Excel was ignoring the fact that the highlighted declare statements are in a conditional construct. All posts I have seen so far suggest just to ignore the error message, however, I cant seem to find a way to convince Excel to continue after the error message pops up. Is there a way to suppress it somehow?
 
Upvote 0
This works for me, using Win10 Pro, Office365 64bit:

Code:
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  Win64 Then
    Private Declare PtrSafe Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPtr
    Private Declare PtrSafe Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As LongPtr, lpExitCode As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If

Are you perhaps still testing #If Win64 = True? That will throw a compile error.
 
Upvote 0
Yes, I was still testing #If Win64 = True. Without the "= True", it works!
Thanks a lot for your help.
I am just wondering why the "=True" is not needed? BTW, the code also seems to work with testing #If VBA7, I just need to leave out the "= True" part.
 
Upvote 0

Forum statistics

Threads
1,224,899
Messages
6,181,629
Members
453,058
Latest member
rmd0725

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top