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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:33:53  来源:igfitidea点击:

What are the differences between VBA 6.0 and VBA 7.0?

vbams-officeoffice-2010

提问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 位版本(有关这些差异的内容,请参见下文)。以下是主要变化:

  1. 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 If
    • If 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
  2. To support this, there are:

    • Three new keywords(2 data types and 1 modifier): LongPtr, LongLongand PtrSafe

    • One new function: CLngLng()(i.e. Int64)

    • The new compilation constants as used above: VBA7and Win64

  1. 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. 为了支持这一点,有:

    • 三个新关键字(2 个数据类型和 1 个修饰符):LongPtr,LongLongPtrSafe

    • 一个新函数:(CLngLng()即 Int64)

    • 上面使用的新编译常量VBA7Win64

回答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.

完整的更改列表仍然会有所帮助...和/或文件转换器。