Professional Documents
Culture Documents
In this article
This content is no longer actively maintained. It is provided as is, for anyone who may still be using these
technologies, with no warranties or claims of accuracy with regard to the most recent product version or
service release.
Summary: Learn to use Microsoft Visual Basic for Applications code in the 32-bit and 64-bit versions of
Microsoft Office 2010.
Contents
Working with the 32-bit Version and 64-bit Version of Office 2010
Conclusion
Additional Resources
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.
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.
VBA Copy
#Else
' API function to locate a window.
Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" ( _
ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Sub DisplayExcelWindowSize()
Dim hwnd As Long, uRect As RECT
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.
VBA Copy
' 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.
Conclusion
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.