Office Talk: Working with VBA in the 32-bit and 64-bit Versions of Office 2010
Summary: Learn to use Microsoft Visual Basic for Applications code in the 32-bit and 64-bit versions of Microsoft Office 2010.
Applies to: Microsoft Office 2010
Published: May 2010
Provided by: Frank Rice, Microsoft Corporation
Office 2010 32-bit and 64-bit Client Installations
Microsoft offers a 32-bit client installation and 64-bit client installation of Microsoft Office 2010. If you use a 64-bit version of Microsoft Windows, you can choose which version of Microsoft Office to use. By using the 64-bit version, you can experience improved application performance by using more physical memory for storage and by moving more data in fewer operations.
However, there are considerations when moving your 32-bit applications to or creating solutions for the 64-bit version of Microsoft Office. For example, applications written for the 64-bit version likely do not work in previous versions of Microsoft Office. Also, calls to Microsoft Windows Application Programming Interface (API) functions from your application code may not work.
Calls to 64-bit Windows API functions from 32-bit solutions (and the opposite) may lead to erratic behavior or system crashes resulting from the truncation of data or overflows into protected memory spaces. To compensate for this, Microsoft Office provides a 32-bit version and 64-bit version of several of the Declare statements used in the Windows API. To ensure compatibility, you must change the Declare statements in solutions to differentiate between 32-bit calls and 64-bit calls. These differences and remedies are described in the article Compatibility Between the 32-bit and 64-bit Versions of Office 2010.
Office 2010 introduces a new version of Microsoft Visual Basic for Applications (VBA) known as VBA 7.0 that is updated to work with 64-bit client installations. VBA 7.0 improves the performance of your Microsoft Office applications through 64-bit performance improvements. VBA 7.0 also enables you to create applications that are compatible with Office 2007 or continue to use VBA solutions created in previous versions. To improve compatibility of application code targeted to a particular version of Microsoft Office, there is a compilation constant (VBA7) that enables you to determine the version of VBA you are using and execute the correct code. Note that if your applications are only written for the 32-bit versions of Microsoft Office, no changes are necessary to your code.
There is also a compilation constant (Win64) that enables your Office 2010 solutions to work with both the 32-bit version and 64-bit version. Both of these compilation constants are demonstrated in the following sections.
Working with Different Versions of VBA
As stated previously, VBA 7.0 works with 64-bit versions of Microsoft Office. To enable VBA solutions created for 32-bit versions to continue working in Office 2010, the compilation constant (VBA7) tests to determine the VBA version used in the solution.
The following code example shows how to use the VBA7 constant. This example enables you to find the location and size of the main window in Microsoft Excel 2010. To do this, first, a user-defined type, RECT, is created. Next, the Windows API functions, FindWindow and GetWindowRect, are defined to return the dimensions of the window. Because there is a 32-bit version and 64-bit version of these functions, the VBA7 compilation constant directs the compiler to the correct section of code. Then, the DisplayExcelWindowSize subroutine is called. This subroutine calls the FindWindow and GetWindowRect functions. Finally, a message box appears with the dimensions.
' A user-defined type to store the window dimensions. Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type ' Test which version of VBA you are using. #If VBA7 Then ' API function to locate a window. Declare PtrSafe Function FindWindow Lib "user32" _ Alias "FindWindowA" ( _ ByVal lpClassName As String, _ ByVal lpWindowName As String) As LongPtr ' API function to retrieve a window's dimensions. Declare PtrSafe Function GetWindowRect Lib "user32" ( _ ByVal hwnd As LongPtr, _ lpRect As RECT) As Long #Else ' API function to locate a window. Declare Function FindWindow Lib "user32" _ Alias "FindWindowA" ( _ ByVal lpClassName As String, _ ByVal lpWindowName As String) As Long ' API function to retrieve a window's dimensions. Declare Function GetWindowRect Lib "user32" ( _ ByVal hwnd As Long, _ lpRect As RECT) As Long #End If Sub DisplayExcelWindowSize() Dim hwnd As Long, uRect As RECT ' Get the handle identifier of the main Excel window. hwnd = FindWindow("XLMAIN", Application.Caption) ' Get the window's dimensions into the RECT UDT. GetWindowRect hwnd, uRect ' Display the result. MsgBox "The Excel window has these dimensions:" & _ vbCrLf & " Left: " & uRect.Left & _ vbCrLf & " Right: " & uRect.Right & _ vbCrLf & " Top: " & uRect.Top & _ vbCrLf & " Bottom: " & uRect.Bottom & _ vbCrLf & " Width: " & (uRect.Right - uRect.Left) & _ vbCrLf & " Height: " & (uRect.Bottom - uRect.Top) End Sub
To test this solution, add a module to Excel 2010, insert the previous code, and then run the macro. You should see a message displayed similar to Figure 1 with the dimensions, in pixels, of the window.
Figure 1. Message with the dimensions of the main Excel window
Resize the Excel window and rerun the macro. You should see that the values changed.
Working with the 32-bit Version and 64-bit Version of Office 2010
In some solutions, you must provide code for the 32-bit version of Office 2010 and code for the 64-bit version of Office 2010. And as in the previous example, you must call different versions of the Windows API functions. To do this, the Win64 compilation constant is available as shown in the following example.
' Test whether you are using the 64-bit version of Office 2010. #If Win64 Then Declare PtrSafe Function GetTickCount64 Lib "kernel32" () As LongLong #Else Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long #End If
In this example, if you code is running in the 64-bit version of , the GetTickCount64 function is called. Otherwise, the GetTickCount function is called.
VBA 7.0 is the latest version of VBA that improves the performance of applications that are created in Office 2010. To enable you to continue using your legacy solutions in different versions of Microsoft Office, the VBA7 compilation constant is available. Likewise, because there is now a 32-bit version and 64-bit version of Office 2010, you use the Win64 compilation constant to direct the compiler to the run the correct section of code in your VBA applications.
For more information, see the following: