VBA 早期与晚期绑定 - 现实生活中的性能差异 [英] VBA Early vs Late binding - real life performance differences

查看:28
本文介绍了VBA 早期与晚期绑定 - 现实生活中的性能差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sub HeadingDefinitionWords_test()
    Application.ScreenUpdating = False
    Dim tm As Long
    tm = timeGetTime
    Dim DefinitionRangeBackup As Range, DefinitionRange As Range
    Dim kEy As Variant, i As Long, k As Integer
    Dim TempList As Object: Set TempList = CreateObject("Scripting.Dictionary")
    'Dim TempList As Scripting.Dictionary: Set TempList = New Scripting.Dictionary
    For i = 1 To 1000
    'Call HeadingDefinitionWords(Selection.Range.Duplicate, TempList)
    '''
         Set DefinitionRange = Selection.Range.Duplicate
         Set DefinitionRangeBackup = DefinitionRange.Duplicate
         '-
            With DefinitionRange.Find: .ClearFormatting: .Text = "([a-z])([A-Z])"
                .Forward = True: .Wrap = wdFindStop: .Format = False: .MatchCase = False: .MatchWholeWord = False: .MatchAllWordForms = False
                .MatchSoundsLike = False: .MatchWildcards = True
            End With
        '-
            With DefinitionRange: While .Find.Execute And .InRange(DefinitionRangeBackup)
            '-
                .Expand Unit:=wdWord
            '-
                If Not TempList.Exists(Trim(DefinitionRange.Text)) Then TempList.Add Trim(DefinitionRange.Text), Trim(DefinitionRange.Text)
            '-
                DefinitionRange.Collapse wdCollapseEnd
            Wend: End With
        
        '''
        For Each kEy In TempList
            'Debug.Print kEy
            If k = 50 Then
                'Debug.Print k
                k = 1
            Else
                k = k + 1
            End If
            'DoEvents
        Next
    
    Next
    
    Dim tma As Long
    tma = timeGetTime
    Debug.Print tma - tm
    
    Application.ScreenUpdating = True
End Sub

推荐答案

不确定这应该作为答案,但是嘿,

Not sure this should go as answer but hey,

早期绑定是开发工作的首选(速度和智能感知).正是出于您提到的原因,后期绑定经常用于分发.在生产中使用后期绑定要安全得多,除非您可以绝对 100% 确定您的用户设置.

Early binding is preferred for development work (speed and intellisense). Late binding is frequently used for distribution for exactly the reasons you mention. Far safer to go with Late binding in production unless you can be absolutely 100% certain of your users set-ups.

引自 Word MVP 网站

早期绑定的优势

您的代码将运行得更快,因为它可以全部预先编译.使用后期绑定,与您声明为对象的应用程序相关的代码实际上必须在运行时进行编译.

Your code will run considerably faster, because it can all be compiled up front. With late binding, the code relating to an application you declared as an object has to, in effect, be compiled as it runs.

因为您的代码都可以预先编译,所以调试要容易得多——选择调试 + 编译,编译器将能够发现如果您使用后期绑定可能会遗漏的语法错误.

Because your code can all be compiled up front, debugging is far easier – select Debug + Compile, and the compiler will be able to spot syntax errors which would have been missed had you used late binding.

您在项目中拥有对智能感知的完全访问权限(键入一个关键字和一个点以获取该关键字支持的属性和方法的弹出列表,选择一个进行插入;键入一个关键字并按 F1 启动帮助主题在那个关键字上).

You have full access in your project to intellisense (type a keyword and a dot to get a popup list of properties and methods supported by that keyword, select one to insert it; type a keyword and press F1 to launch the Help topic on that keyword).

您可以通过对象浏览器和 VBA 帮助完全访问应用程序的对象模型.

You have full access to the application's object model via the Object Browser and VBA Help.

您可以访问应用程序的内置常量.例如,如果您要从 Excel 自动化 Word,您可以使用:

You have access to the application's built-in constants. For instance, if you are automating Word from Excel, you can use:

Dim objWord As Word.Application 
Set objWord = New Word.Application 

With objWord 
    .Visible = True 
    .Activate 
    .WindowState = wdWindowStateMaximize 
    .Documents.Open ("c:	emp	emp.doc") 
End With 

此外,当你输入

.WindowState =

您将获得支持常量的弹出列表,并且可以简单地从列表中选择 wdWindowStateMaximize.

you'll get a pop-up list of the supported constants, and can simply pick wdWindowStateMaximize from the list.

如果您使用后期绑定,则需要使用:

If you used late binding, you would need to use:

.WindowState = 1

.. 你需要知道(通过在 Word 的对象浏览器中查找)常量 "wdWindowStateMaximize" 的值恰好是 1.

.. and you would need to know (by looking it up in Word's Object Browser) that the value of the constant "wdWindowStateMaximize" happens to be 1.

所有这些使得使用早期绑定的编程比使用后期绑定要容易得多.

All this makes programming using early binding immeasurably easier than using late binding.

后期绑定的优势

主要优点是使用后期绑定的代码更确定与版本无关

The main advantage is that code which uses late binding is more certain to be version-independent

如果您将 Word 97 项目中的引用设置为Microsoft Excel 8.0 对象库",则该项目将在安装了 Office 2000 的机器上正常运行.Word 2000 即时将引用更改为Microsoft Excel 9.0 对象库".

If you set a reference in a Word 97 project to "Microsoft Excel 8.0 Object Library", then the project will run OK on a machine which has Office 2000 installed. Word 2000 changes the reference on the fly to the "Microsoft Excel 9.0 Object Library".

但正如他们所说,YMMV.在某些情况下发现了问题.例如,如果您在安装了 Office 2000 的计算机上运行包含对 Excel 8.0 对象库的引用的 Word 97 项目,它将运行正常,但除非您保存该项目,否则您可能偶尔会遇到无法打开宏存储"错误在 Word 2000 中.如果将其保存在 Word 2000 中,则引用将更改为 Excel 9.0 对象库.

But as they famously say, YMMV. Problems have been found in certain circumstances. For instance, if you run a Word 97 project containing a reference to the Excel 8.0 object library on a machine with Office 2000 installed, it will run OK, but you may get the occasional "cannot open macro storage" error unless you save the project in Word 2000. If you do save it in Word 2000, the reference will change to the Excel 9.0 object library.

因此,如果您使用早期绑定并支持混合环境,那么尽管存在维护开销,创建单独的 Word 97 和 Word 2000 版本的加载项可能是最安全的.

So if you use early binding and support a mixed environment, it may be safest to create separate Word 97 and Word 2000 versions of your addins, despite the maintenance overhead.

您的项目包含的引用越多,文件越大,编译所需的时间就越长.

The more references your project contains, the larger the file size and the longer it takes to compile.

某些编程环境不允许您创建对另一个应用程序的引用.

Some programming environments don't allow you to create references to another application.

如果您真的想开始深入研究 COM 对象、v-tables 等,可以从以下参考开始:

If you really want to start delving into COM objects, v-tables and the like maybe start with the following reference:

参考文献:

  1. Microsoft Blurb:在自动化中使用早期绑定和后期绑定

这篇关于VBA 早期与晚期绑定 - 现实生活中的性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆