由于用户安装了较早版本的MS Office(MS Outlook),防止Excel VBA编译错误? [英] Preventing Excel VBA compile errors due to users having an older version of MS Office (MS Outlook) installed?

查看:116
本文介绍了由于用户安装了较早版本的MS Office(MS Outlook),防止Excel VBA编译错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个电子表格,我已经从VBA编辑器和仅安装了MS Outlook 12.0的用户引用了MS Outlook 14.0对象库,那么当该用户打开该电子表格时,他们在此行上收到编译错误:

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:

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对象库

MS Outlook 12.0 Object Library

...代码然后正确编译,电子表格适用于他们。

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

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

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.

任何人都可以建议一个更好的方式来处理与旧版本的MS Office的向后兼容性问题?

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

推荐答案

tbone,你所说的强打字被称为早期绑定。

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

不幸的是,Early Binding的一个缺点是如果最终用户与您的版本不一样,那么您将收到这些错误。 em>

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.

如果你问我,我喜欢Late Binding(你不创建引用并使用CreateObject创建一个实例)

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

一个有趣的阅读。

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

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

我的建议

如果你喜欢它,因为intellisense放弃早期绑定。然而,在分发应用程序之前,请将代码更改为Latebinding。在早期绑定和延迟绑定中的代码方式没有太大的区别。

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.

这里是一个例子

早期绑定

'~~> 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

/ strong>

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

Sid

这篇关于由于用户安装了较早版本的MS Office(MS Outlook),防止Excel VBA编译错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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