#If Vba7 Then ' It's important to check for Win64 first, ' because Win32 will also return true when Win64 does. #If Win64 Then Declare PtrSafe Function GetFoo64 Lib "exampleLib32" () As LongLong #Else Declare PtrSafe Function GetFoo Lib "exampleLib32" () As Long #End If #Else ' Must be Vba6, the PtrSafe keyword didn't exist back then, ' so we need to declare Win32 imports a bit differently than above. #If Win32 Then Declare Function GetFoo Lib "exampleLib32"() As Long #Else Declare Function GetFoo Lib "exampleLib"() As Integer #End If #End If
根据您需要支持的Office版本,可以对此进行简化。例如,仍然没有多少人支持Office的16位版本。16位Office的最新版本是1994年发布的4.3版,因此以下声明对于几乎所有现代情况(包括Office 2007)都是足够的。
#If Vba7 Then ' It's important to check for Win64 first, ' because Win32 will also return true when Win64 does. #If Win64 Then Declare PtrSafe Function GetFoo64 Lib "exampleLib32" () As LongLong #Else Declare PtrSafe Function GetFoo Lib "exampleLib32" () As Long #End If #Else ' Must be Vba6. We don't support 16 bit office, so must be Win32. Declare Function GetFoo Lib "exampleLib32"() As Long #End If
如果您不需要支持Office 2010之前的任何版本,则此声明可以正常工作。
' We only have 2010 installs, so we already know we have Vba7. #If Win64 Then Declare PtrSafe Function GetFoo64 Lib "exampleLib32" () As LongLong #Else Declare PtrSafe Function GetFoo Lib "exampleLib32" () As Long #End If