I have observed an issue with below function call when we migrated to office 2010-64 bit version.
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
According to information available on http://msdn.microsoft.com/en-us/library/ee691831.aspx link. I have changed above call as below and it has been working fine on office 2010 64 bit version.
Private Declare PtrSafe Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
The problem is, I need to make same call to work on older office versions as well and it throws compile error on older versions.
Has anyone any idea how to make this call working for office 2010 and older office versions.
In Microsoft Office 2010, VBA includes language features that enable VBA code to run correctly in both 32-bit and 64-bit environments. By default, Office 2010, 2013 and 2016 install the 32-bit version. You must explicitly choose to install the 64-bit version during setup.
Microsoft Visual Basic for Applications 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 issue only occurs when you edit the VBA macro. This issue does not occur when you run the macro.
The 32-bit and 64-bit versions of Office programs aren't compatible, so you can't install both on the same computer.
The files created by 32-bit and 64-bit Excel are interchangeable and indistinguishable. Both versions of the program produce the same files. Both open the same files.
As the MSDN article says, use conditional compilation: it works well for me in Excel 97 through Excel 2010 32-bit & 64-bit.
#If VBA7 Then
Private Declare PtrSafe Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
#Else
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
#End if
I find the use of this VBA7 compiler constant all over the Internet in relation to Office 64-bit compatibility but contrary to what is often said, this compiler constant detects Office installs using VBA7 e.g. Office 2010 onwards, not 64-bit Office. Confusingly, if you need to determine if you're using the 64-bit version of Office application, you need to use the Win64 constant!
Try this out of 32 and 64 bit versions of Office and you can see what I mean:
Sub DemoCompilerConstants()
#If VBA7 Then
MsgBox "VBA7"
#Else
MsgBox "Not VBA7"
#End If
#If Win64 Then
MsgBox "Win64"
#Else
MsgBox "NOT Win64"
#End If
End Sub
Thanks to Steve Rindsberg for putting me straight on this one! Steve also added:
Win64 actually tells you whether or not your version of your Office app is 64-bit. If you have to support older versions of Office, you may want to combine that with a VBA7 check to special-case versions of your Office app that won't understand the newer compiler directives. But if your code's failing on a 32-bit version of the Office app, it may well be because it's hitting your compiler directive, finding that yes, it's got VBA7 and then trying to exec the 64-bit API call, which won't fly because it's 32-bit Office.
So this should be the correct approach for combining VBA7 and 64-bit Office compatibility:
#If VBA7 Then
#If Win64 Then ' Declare using PtrSafe for Office 64 bit
Declare PtrSafe Function ....
#Else ' Declare for Office 32 bit
Declare Function ....
#End If
#Else ' Declare for Office 32 bit
Declare Function ....
#End If
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With