'Why does my `#If Not Win64 Then` code get executed on 32-bit Office?
I have the following code which I only want executed on 32-bit Office (not 64-bit Office):
#If Not Win64 Then
DoSomethingOnlyIn32BitOffice
#End If
According to the documentation, the compile-time constant Win64 should be True in 64-bit Office. However, debugging this code in 64-bit Office (tested with 2016) confirms that DoSomethingOnlyIn32BitOffice is actually called. What did I do wrong?
Solution 1:[1]
You found a bug in the documentation.
The documentation claims that the VBA compile-time constants Win64, Win32, etc. are initialized to either True of False. This, however, seems to be incorrect. Consider the following example code:
#If Win32 Then
Debug.Print "Win32"
#End If
#If Not Win32 Then
Debug.Print "Not Win32"
#End If
#If Win32 = True Then
Debug.Print "Win32 = True"
#End If
#If Win32 = False Then
Debug.Print "Win32 = False"
#End If
#If Win32 = 1 Then
Debug.Print "Win32 = 1"
#End If
#If Win32 = 2 Then
Debug.Print "Win32 = 2"
#End If
which yields the following result in Office 2016 (32-bit):
Win32
Not Win32
Win32 = 1
Apparently, those constants are set to 1 when "truth-y", not to -1 (= the VBA True value). This is inconvenient, since it leads to both #If Not Win32 and #If Win32 = True doing something completely different than the developer intended.
My suggested fix would be to write
#If Win64 Then
' No action needed in 64-Bit Office
#Else
DoSomethingOnlyIn32BitOffice
#End If
instead, since all the other options (Win64 <> 1, etc.) rely on the (undocumented) fact that the "truth-y" value is 1.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
