64 位元 Visual Basic for Applications 概觀64-bit Visual Basic for Applications overview

Microsoft Visual Basic for Applications (VBA) 是與 Microsoft Office 所一起發行的 Visual Basic 版本。Microsoft Visual Basic for Applications (VBA) is the version of Visual Basic that ships with Microsoft Office. 在 Microsoft Office 2010 中,VBA 包含讓 VBA 程式碼在 32 位元和 64 位元環境中,都能正確地執行的的語言功能。In Microsoft Office 2010, VBA includes language features that enable VBA code to run correctly in both 32-bit and 64-bit environments.

注意

根據預設,Office 2010 會安裝 32 位元版本。By default, Office 2010 installs the 32-bit version. 您必須在安裝期間明確地選擇要安裝 64 位元版本。You must explicitly choose to install the 64-bit version during setup.

如果程式碼沒有修改以便在 64 位元版本的 Office 中執行,則在 64 位元平台上執行 Office 2010 之前版本所撰寫的 VBA 程式碼 (VBA 6 或更早的版本),可能會導致錯誤。Running VBA code that was written before the Office 2010 release (VBA version 6 and earlier) on a 64-bit platform can result in errors if the code is not modified to run in 64-bit versions of Office. 因為 VBA 6 或更早的版本是隱含以 32 位元平台為目標,而且通常包含 Declare 陳述式,會使用指標和控制代碼的 32 位元資料類型來呼叫 Windows API,因此將產生錯誤。Errors will result because VBA version 6 and earlier implicitly targets 32-bit platforms, and typically contains Declare statements that call into the Windows API by using 32-bit data types for pointers and handles. 由於 VBA 第 6 版或更早的版本並沒有指標或控制代碼的特定資料類型,因此會使用 Long 資料類型 ( 32 位元 4 個位元組的資料類型),來參考指標和控制代碼。Because VBA version 6 and earlier does not have a specific data type for pointers or handles, it uses the Long data type, which is a 32-bit 4-byte data type, to reference pointers and handles. 在 64 位元環境中,指標和控制代碼是 8 位元組 64 位元的數量。Pointers and handles in 64-bit environments are 8-byte 64-bit quantities. 32 位元資料類型中,無法保存這些 64 位元數量。These 64-bit quantities cannot be held in 32-bit data types.

注意

只有在 Microsoft Office 的 64 位元版本中執行時,您才需要修改 VBA 程式碼。You only need to modify VBA code if it runs in the 64-bit version of Microsoft Office.

在 64 位元的 Office 執行舊版 VBA 程式碼的問題是,若嘗試將 64 位元載入到 32 位元資料類型時,會截斷 64 位元數量。The problem with running legacy VBA code in 64-bit Office is that trying to load 64-bits into a 32-bit data type truncates the 64-bit quantity. 這可能會導致記憶體滿溢、程式碼中未預期的結果,以及可能的應用程式失敗。This can result in memory overruns, unexpected results in your code, and possible application failure.

若要解決這個問題,並讓 VBA 程式碼在 32 位元和 64 位元的環境中正常運作,已新增許多語言功能到 VBA 之中。To address this problem and enable VBA code to work correctly in both 32-bit and 64-bit environments, several language features have been added to VBA. 這份文件底部的表格摘要說明了新的 VBA 語言功能。The table at the bottom of this document summarizes the new VBA language features. 三個重要的新增部分是 LongPtr 類型別名、LongLong 資料類型,以及 PtrSafe 關鍵字。Three important additions are the LongPtr type alias, the LongLong data type, and the PtrSafe keyword.

  • LongPtrLongPtr. VBA 現在包含變數的類型別名 LongPtrVBA now includes the variable type alias LongPtr. LongPtr 所解析的目標實際資料類型是根據其所執行的 Office 版本;LongPtr 以 32 位元版本的 Office 解析為 Long,且 LongPtr 以 64 位元版本的 Office 解析為 LongLongThe actual data type that LongPtr resolves to depends on the version of Office that it is running in; LongPtr resolves to Long in 32-bit versions of Office, and LongPtr resolves to LongLong in 64-bit versions of Office. LongPtr 用於指標和控制代碼。Use LongPtr for pointers and handles.

  • LongLongLongLong. LongLong 是帶正負號的 64 位元整數資料類型,只可以在 64 位元版本的 Office 中使用。The LongLong data type is a signed 64-bit integer that is only available on 64-bit versions of Office. LongLong 用於64 位元整數。Use LongLong for 64-bit integrals. 轉換函數必須用來將 LongLong (包括在 64 位元平台上的 LongPtr ) 明確地指派為較小的整數類型。Conversion functions must be used to explicitly assign LongLong (including LongPtr on 64-bit platforms) to smaller integral types. 不允許將 LongLong 隱含轉換為較小的整數。Implicit conversions of LongLong to smaller integrals are not allowed.

  • PtrSafePtrSafe. PtrSafe 關鍵字會判斷提示 Declare 陳述式是否能夠安全地在 64 位元版本的 Office 中執行。The PtrSafe keyword asserts that a Declare statement is safe to run in 64-bit versions of Office.

重要

所有 Declare 陳述式,在 64 位元版本的 Office 中執行時,現在都必須包含 PtrSafe 關鍵字。All Declare statements must now include the PtrSafe keyword when running in 64-bit versions of Office. 請務必了解,僅將 PtrSafe 關鍵字新增至 Declare 陳述式只表示 Declare 陳述式明確地將目標設定為 64 位元。It is important to understand that simply adding the PtrSafe keyword to a Declare statement only signifies that the Declare statement explicitly targets 64-bits. 必須儲存 64 位元 (包括傳回值和參數) 的陳述式內所有資料類型仍必須修改為存放 64 位元的數量。All data types within the statement that need to store 64-bits (including return values and parameters) must still be modified to hold 64-bit quantities.

注意

具有 PtrSafe 關鍵字的 Declare 陳述式是建議的語法。Declare statements with the PtrSafe keyword is the recommended syntax. 包括 PtrSafeDeclare 陳述式,在 32 位元和 64 位元的平台上的 VBA7 開發環境中,都可以正確運作。Declare statements that include PtrSafe work correctly in the VBA7 development environment on both 32-bit and 64-bit platforms.

若要確保回溯為 VBA7 或更早版本的相容性,請使用下列建構︰To ensure backwards compatibility in VBA7 and earlier use the following construct:

 #If VBA7 Then 
 Declare PtrSafe Sub... 
 #Else 
 Declare Sub... 
 #EndIf

請考慮下列 Declare 陳述式範例。Consider the following Declare statement examples. 如果在 64 位元版本的 Office 中執行未修改的 Declare 陳述式會導致錯誤,這表示 Declare 陳述式中不包括 PtrSafe 限定詞。Running the unmodified Declare statement in 64-bit versions of Office will result in an error indicating that the Declare statement does not include the PtrSafe qualifier. 已修改的 VBA 範例包含了 PtrSafe 限定詞,但請注意,傳回的值 (使用中視窗的指標) 傳回了 Long 資料類型。The modified VBA example contains the PtrSafe qualifier, but notice that the return value (a pointer to the active window) returns a Long data type. 在 64 位元版本的 Office 中這不正確,因為指標是 64 位元。On 64-bit Office, this is incorrect because the pointer needs to be 64-bits. PtrSafe 限定詞會告訴編譯器 Declare 陳述式是以 64 位元為目標,因此不會發生錯誤的陳述式執行。The PtrSafe qualifier tells the compiler that the Declare statement is targeting 64-bits, so the statement executes without error. 但因為傳回的值尚未更新為 64 位元資料類型,所以傳回值會截斷,導致傳回值不正確。But because the return value has not been updated to a 64-bit data type, the return value is truncated, resulting in an incorrect value returned.

以下是未修改的舊版 VBA Declare 陳述式範例︰Following is an unmodified legacy VBA Declare statement example:

Declare Function GetActiveWindow Lib "user32" () As Long

以下 VBA Declare 陳述式範例修改為包括 PtrSafe 限定詞,但仍在使用 32 位元的傳回值︰The following VBA Declare statement example is modified to include the PtrSafe qualifier but still use a 32-bit return value:

Declare PtrSafe Function GetActiveWindow Lib "user32" () As Long

再次提醒您,您必須修改 Declare 陳述式以包含 PtrSafe 限定詞,且您必須更新陳述式內任何需要保留 64 位元數量的變數,以便使變數使用 64 位元資料類型。To reiterate, you must modify the Declare statement to include the PtrSafe qualifier, and you must update any variables within the statement that need to hold 64-bit quantities so that the variables use 64-bit data types.

以下是 VBA Declare 陳述式範例修改為包括 PtrSafe 關鍵字和更新,以使用適當的 64 位元 (LongPtr) 資料類型︰Following is a VBA Declare statement example that is modified to include the PtrSafe keyword and is updated to use the proper 64-bit (LongPtr) data type:

Declare PtrSafe Function GetActiveWindow Lib "user32" () As LongPtr

總而言之,若要讓程式碼在 64 位元版本的 Office 中運作,您需要找出並修改所有現有的 Declare 陳述式,以使用 PtrSafe 限定詞。In summary, for code to work in 64-bit versions of Office, you need to locate and modify all existing Declare statements to use the PtrSafe qualifier. 您還需要找出並修改這些參考指標和控制代碼的 Declare 陳述式內的所有資料類型,以使用新的 64 位元相容的 LongPtr 類型別名,而且需要以新的 LongLong 資料類型來保留 64 位元整數。You also need to locate and modify all data types within these Declare statements that reference handles or pointers to use the new 64-bit compatible LongPtr type alias, and types that need to hold 64-bit integrals with the new LongLong data type. 此外,您必須更新任何包含指標或控制代碼和 64 位元整數的使用者定義類型 (UDT),以使用 64 位元資料類型,並確認所有變數的指派都是正確的,來避免類型不符的錯誤。Additionally, you must update any user defined types (UDTs) that contain pointers or handles and 64-bit integrals to use 64-bit data types, and verify that all variable assignments are correct to prevent type mismatch errors.

撰寫在 32 位元和 64 位元的 Office 上都能運作的程式碼Writing code that works on both 32-bit and 64-bit Office

若要撰寫可在 32 位元和 64 位元的 Office之間移植的程式碼,您只需要針對所有的指標和控制代碼值,使用新的 LongPtr 類型別名,而不是 LongLongLongTo write code that can port between both 32-bit and 64-bit versions of Office, you only need to use the new LongPtr type alias instead of Long or LongLong for all pointers and handle values. 根據執行 LongPtr 類型別名的Office 版本,會解析為正確的 LongLongLong 資料類型。The LongPtr type alias will resolve to the correct Long or LongLong data type depending on which version of Office is running.

請注意,如果您需要執行不同的邏輯 (例如您需要操縱大型的 Excel 專案中的 64 位元值),您可以使用 Win64 條件式編譯常數,如下一節中所示。Note that if you require different logic to execute, for example, you need to manipulate 64-bit values in large Excel projects, you can use the Win64 conditional compilation constant as shown in the following section.

撰寫在 Office 2010 (32 位元或 64 位元) 及先前的 Office 版本中都能運作的程式碼Writing code that works on both Office 2010 (32-bit or 64-bit) and previous versions of Office

若要撰寫在新的和較舊版本的 Office 中都能運作的程式碼,您可以使用新的 VBA7Win64 條件式 編譯器常數組合。To write code that can work in both new and older versions of Office, you can use a combination of the new VBA7 and Win64 conditional Compiler constants. Vba7 條件式編譯器常數,是用來判斷程式碼是否正在第 7 版 VB 編輯器中執行 (在 Office 2010 發行的 VBA 版本)。The Vba7 conditional compiler constant is used to determine if code is running in version 7 of the VB editor (the VBA version that ships in Office 2010). Win64 條件式編譯常數,是用來判斷所執行的 Office 哪一個版本 (32 位元或 64 位元)。The Win64 conditional compiler constant is used to determine which version (32-bit or 64-bit) of Office is running.

#if Vba7 then 
'  Code is running in the new VBA7 editor 
     #if Win64 then 
     '  Code is running in 64-bit version of Microsoft Office 
     #else 
     '  Code is running in 32-bit version of Microsoft Office 
     #end if 
#else 
' Code is running in VBA version 6 or earlier 
#end if 
 
#If Vba7 Then 
Declare PtrSafe Sub... 
#Else 
Declare Sub... 
#EndIf 

VBA7 語言更新的摘要Summary of VBA7 language updates

下表摘要說明新 VBA 語言每個新增的項目,並提供說明。The following table summarizes the new VBA language additions and provides an explanation of each.

名稱Name 類型 Type 描述Description
PtrSafePtrSafe 關鍵字Keyword 判斷提示 Declare 陳述式是否以 64 位元系統為目標。Asserts that a Declare statement is targeted for 64-bit systems. 在 64 位元系統中為必要。Required on 64-bits.
LongPtrLongPtr 資料類型Data type 在 32 位元系統上對應至 Long,或在 64 位元系統上對應至 LongLong 的類型別名。Type alias that maps to Long on 32-bit systems, or LongLong on 64-bit systems.
LongLongLongLong 資料類型Data type 8 位元組資料類型別只有在 64 位元系統上可用。8-byte data type that is only available on 64-bit systems. 數字類型。Numeric type. 在以下範圍內的整數數字:9,223,372,036,854,775,808 to 9,223,372,036,854,775,807。Integer numbers in the range of -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

LongLong 只有在 64 位元平台上是有效的宣告類型。LongLong is a valid declared type only on 64-bit platforms. 此外,LongLong 可能不會隱含地轉換成較小的類型 (例如,您不能將 LongLong 指派給 Long)。Additionally, LongLong may not be implicitly converted to a smaller type (for example, you can't assign a LongLong to a Long). 這麼做是為了避免指標不小心遭截斷。This is done to prevent inadvertent pointer truncation.

允許明確的強制型轉,如此在上述範例中,您可以將 CLng 套用到 LongLong,並將結果指派給 Long (僅限 64 位元平台上有效)。Explicit coercions are allowed, so in the previous example, you could apply CLng to a LongLong and assign the result to a Long (valid on 64-bit platforms only).
^ LongLong 類型宣告字元LongLong type-declaration character 明確宣告為常值 LongLongExplicitly declares a literal value as a LongLong. 要宣告 LongLong 常值大於 Long 的最大值時為必要 (否則就會隱含轉換成 double)。Required to declare a LongLong literal that is larger than the maximum Long value (otherwise it will get implicitly converted to double).
CLngPtrCLngPtr 類型轉換函數type conversion function 將簡單運算式轉換成 LongPtr 資料類型。Converts a simple expression to a LongPtr.
CLngLngCLngLng 類型轉換函數type conversion function 將簡單運算式轉換成 LongLong 資料類型 (僅限 64 位元平台上有效)。Converts a simple expression to a LongLong data type (valid on 64-bit platforms only).
vbLongLongvbLongLong VarType 常數VarType constant LongLong 整數 (僅限 64 位元平台上有效)。LongLong integer (valid on 64-bit platforms only).
DefLngPtrDefLngPtr DefType 陳述式DefType statement 將範圍中變數的預設資料型設為 LongPtrSets the default data type for a range of variables as LongPtr.
DefLngLngDefLngLng DefType 陳述式DefType statement 將範圍中變數的預設資料型設為 LongLongSets the default data type for a range of variables as LongLong.

另請參閱See also

支援和意見反應Support and feedback

有關於 Office VBA 或這份文件的問題或意見反應嗎?Have questions or feedback about Office VBA or this documentation? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應Please see Office VBA support and feedback for guidance about the ways you can receive support and provide feedback.