使用适用于所有 Office 版本的 Declare Imports
#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
这可以简化一些,具体取决于你需要支持的办公室版本。例如,仍然没有多少人支持 16 位版本的 Office。 16 位办公室的最后一个版本是 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