VBA 使用在所有版本的Office上均可使用的声明导入
示例
#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版,因此以下声明对于几乎所有现代情况(包括Office2007)都是足够的。
#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如果您不需要支持Office2010之前的任何版本,则此声明可以正常工作。
' 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