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

时间:2022-07-26 06:59:31

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函数。但更重要的是,是否有办法避免这种情况?我唯一能想到的就是不设置引用,只使用类型对象的变量并通过CreateObject(“Outlook.Application”)等实例化,但是我讨厌放弃强类型等等。

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

有人能提出一种更好的方法来处理与旧版本MS Office向后兼容的问题吗?

1 个解决方案

#1


22  

tbone, what you refer to as Strong Typing is 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

Sid

#1


22  

tbone, what you refer to as Strong Typing is 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

Sid