防止由于用户安装了旧版本的 MS Office (MS Outlook) 而导致 Excel VBA 编译错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9491314/
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 12:42:26  来源:igfitidea点击:

Preventing Excel VBA compile errors due to users having an older version of MS Office (MS Outlook) installed?

excelvbaexcel-vba

提问by tbone

If I have a spreadsheet where I've referenced MS Outlook 14.0 Object Library from the VBA editor, and a user who only has MS Outlook 12.0 installed, then when that user opens the spreadsheet, they get a compile error on this line:

如果我有一个电子表格,其中我从 VBA 编辑器中引用了 MS Outlook 14.0 对象库,并且一个用户只安装了 MS Outlook 12.0,那么当该用户打开电子表格时,他们会在这一行收到编译错误:

Range("export_date") = Date - 1

If they go into Tools, References, in the references list, there is this error:

如果他们进入参考列表中的工具,参考,则会出现此错误:

MISSING: MS Outlook 14.0 Object Library

If they deselect that library, and instead select

如果他们取消选择该库,而是选择

MS Outlook 12.0 Object Library

MS Outlook 12.0 对象库

...the code then properly compiles and the spreadsheet works fine for them.

...然后代码正确编译,电子表格对他们来说工作正常。

I don't really understand why it fails on the Date() function, as that is VBA function, not an Outlook function. But even more important, is there a way to avoid this situation? The only thing I can think of is to not set references, and just use variables of type Object and instantiate via CreateObject("Outlook.Application"), etc, but I hate to give up strong typing, etc.

我真的不明白为什么它在 Date() 函数上失败,因为这是 VBA 函数,而不是 Outlook 函数。但更重要的是,有没有办法避免这种情况?我唯一能想到的是不设置引用,只使用 Object 类型的变量并通过 CreateObject("Outlook.Application") 等实例化,但我讨厌放弃强类型等。

Can anyone suggest a superior way to handle this issue of backwards compatibility with older versions of MS Office?

任何人都可以提出一种更好的方法来处理与旧版本的 MS Office 向后兼容的问题吗?

回答by Siddharth Rout

tbone, what you refer to as Strong Typingis called "Early Binding".

tbone,你所说的强类型被称为“早期绑定”。

Unfortunately one of the drawbacks with Early Binding is that if the end user doesn't have the same version as yours then you will get those errors.

不幸的是,早期绑定的缺点之一是,如果最终用户的版本与您的版本不同,那么您将收到这些错误。

If you ask me, I prefer Late Binding (where you don't create references and use CreateObject to create an instance)

如果您问我,我更喜欢后期绑定(您不创建引用并使用 CreateObject 创建实例)

An interesting read.

一个有趣的阅读。

Topic: Using early binding and late binding in Automation

主题:在自动化中使用早期绑定和后期绑定

Link: http://support.microsoft.com/kb/245115

链接http: //support.microsoft.com/kb/245115

My Suggestion

我的建议

Don't give up Early Binding if you like it because of intellisense. However before you distribute your application, change the code to Latebinding. There is not much of a difference in the way you code in Early Binding and Late Binding.

如果您喜欢它,请不要因为智能感知而放弃早期绑定。但是,在分发应用程序之前,请将代码更改为 Latebinding。在早期绑定和后期绑定中的编码方式没有太大区别。

Here is an example

这是一个例子

Early Binding

早期绑定

'~~> Set reference to Excel Object Library
Sub Sample()
    Dim oXLApp As Excel.Application
    Dim oXLBook As Excel.Workbook
    Dim oXLSheet As Excel.Worksheet

    '~~> Create a new instance of Excel
    Set oXLApp = New Excel.Application
    '~~> Add a new workbook
    Set oXLBook = oXLApp.Workbooks.Add
    Set oXLSheet = oXLBook.Worksheets(1)

    '
    '~~> Rest of the code
    '
End Sub

Late Binding

后期绑定

'~~> Doesn't require a reference to Excel Object Library
Sub Sample()
    Dim oXLApp As Object
    Dim oXLBook As Object
    Dim oXLSheet As Object

    '~~> Create a new instance of Excel
    Set oXLApp = CreateObject("Excel.Application")
    '~~> Add a new workbook
    Set oXLBook = oXLApp.Workbooks.Add
    Set oXLSheet = oXLBook.Worksheets(1)
    '
    '~~> Rest of the code
    '
End Sub

HTH

HTH

Sid

锡德