VBA 6.0 和 VBA 7.0 之间有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3072356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the differences between VBA 6.0 and VBA 7.0?
提问by romandas
I noticed that Office 2010 comes with Visual Basic for Applications 7.0. However I can't seem to find much documentation on what changes were made. Does anyone have a summary of the changes, or any resources describing the differences?
我注意到 Office 2010 附带 Visual Basic for Applications 7.0。但是,我似乎找不到关于所做更改的太多文档。有没有人有更改的摘要,或任何描述差异的资源?
采纳答案by Todd Main
There's not a whole lot that has changed between VBA6 and VBA7. VBA7 was introduced to support 64-bit versions of both Office and Windows (see below on what those differences are). Here are the key changes:
VBA6 和 VBA7 之间没有太大变化。引入 VBA7 是为了支持 Office 和 Windows 的 64 位版本(有关这些差异的内容,请参见下文)。以下是主要变化:
64-bit support, primarily for API calls. This is both used to make your code work with your OS/Office version as well as others' (i.e. someone on Office 2003/WinXP)
If you are on a 64-bit version of Windows, but are on a 32-bit version of Office, you can declare API calls like below. .
#If Win64 Then Declare PtrSafe Function GetTickCount64 Lib "kernel32"() As LongLong #Else Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long #End IfIf you are on a 64-bit version of Windows, andare on a 64-bit version of Office, you can declare API calls like: .
#If VBA7 Then Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _ ByVal lpClassName As String, _ ByVal lpWindowName As String) As LongPtr #Else Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal _ lpClassName As String, ByVal lpWindowName As String) As Long #End If
To support this, there are:
Three new keywords(2 data types and 1 modifier):
LongPtr,LongLongandPtrSafeOne new function:
CLngLng()(i.e. Int64)The new compilation constants as used above:
VBA7andWin64
64 位支持,主要用于 API 调用。这既用于使您的代码与您的 OS/Office 版本以及其他人(即 Office 2003/WinXP 上的某人)一起使用
如果您使用的是 64 位版本的 Windows,但使用的是 32 位版本的 Office,则可以像下面这样声明 API 调用。.
#If Win64 Then Declare PtrSafe Function GetTickCount64 Lib "kernel32"() As LongLong #Else Declare PtrSafe Function GetTickCount Lib "kernel32" () As Long #End If如果您使用的是 64 位版本的 Windows,并且使用的是 64 位版本的 Office,则可以声明如下 API 调用:
#If VBA7 Then Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" ( _ ByVal lpClassName As String, _ ByVal lpWindowName As String) As LongPtr #Else Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal _ lpClassName As String, ByVal lpWindowName As String) As Long #End If
为了支持这一点,有:
三个新关键字(2 个数据类型和 1 个修饰符):
LongPtr,LongLong和PtrSafe一个新函数:(
CLngLng()即 Int64)上面使用的新编译常量:
VBA7和Win64
回答by Lunatik
This piece on MSDN has more on the changes in VBA 7 for Office 2010:
MSDN 上的这篇文章有更多关于 VBA 7 for Office 2010 的变化:
回答by buckbova
VBA7 is compatible with 64-bit versions of Office.
VBA7 与 64 位版本的 Office 兼容。
回答by Askjerry
There are other changes as well... I'm having users in the field report that code which functioned properly in 2007 no longer works and shows errors.
还有其他变化......我让现场用户报告说,在 2007 年正常运行的代码不再有效并显示错误。
Example, this works in VBA6 (Excel 2007)
例如,这适用于 VBA6 ( Excel 2007)
PRINT STRING$(80,"=")
mynewdata = MID$(mydata, 15,4)
It prints out a line made of "=" characters as a visual break, then looks at mydata, jumps over 15characters and gets 4of them, the result is stored in mynewdata. It fails in VBA7 (Excel 2010).
它打印出一行由“ =”字符组成的行作为视觉中断,然后查看mydata,跳过15 个字符并获取其中的4个,结果存储在mynewdata 中。它在 VBA7 ( Excel 2010) 中失败。
I did find a potential workaround...
我确实找到了一个潜在的解决方法......
PRINT VBA.STRING$(80,"=")
mynewdata = VBA.MID$(mydata, 15,4)
OR
或者
PRINT VBA.STRING(80,"=")
mynewdata = VBA.MID(mydata, 15,4)
A complete list of changes would still be helpful... and/or a file converter.
完整的更改列表仍然会有所帮助...和/或文件转换器。

