使用適用於所有 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