如何从Excel中选择对MS Outlook用户版本的“引用"? [英] How to select 'Reference' to user's version of MS Outlook from Excel?

查看:122
本文介绍了如何从Excel中选择对MS Outlook用户版本的“引用"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在与 Outlook 对话的 Excel 中运行代码.

I want to run code in Excel, that talks to Outlook.

我可以从VBE中的工具->参考中选择正确的参考.

I can select the right reference from Tools->References in the VBE.

我希望我的代码为其他用户运行.它们将具有不同版本的Outlook和Excel.

I want my code to run for other users. They will have different versions of Outlook and Excel.

有没有办法使代码选择对MS Outlook的正确引用,或者告诉我是否未安装Outlook等?

Is there a way I can make the code select the right reference to MS Outlook, or tell me if Outlook isn't installed, etc.?

推荐答案

我使用了类似的功能,该功能适用​​于Outlook2010.如果您使用的Office版本不同,则可能需要更改路径/参数,或者,如果您必须处理多个版本的Office,则将需要一些其他逻辑来处理版本控制,但这是它的基础.

I use a function like this which should work for Outlook 2010. If you're using a different version of Office you may need to change the path/arguments, or if you have to deal with multiple versions of Office then you will need some additional logic to handle the versioning, but this is the basics of it.

此子例程将添加引用(如果尚不存在)

Sub AddRefToOutlook()
    Const outlookRef as String = "C:\Program Files (x86)\Microsoft Office\Office14\MSOUTL.OLB"

    If Not RefExists(outlookRef, "Microsoft Outlook 14.0 Object Library") Then
        Application.VBE.ActiveVBProject.References.AddFromFile _
            outlookRef
    End If
End Sub

此功能检查引用是否存在

Function RefExists(refPath As String, refDescrip As String) As Boolean
'Returns true/false if a specified reference exists, based on LIKE comparison
' to reference.description.

Dim ref As Variant
Dim bExists As Boolean

'Assume the reference doesn't exist
bExists = False

For Each ref In Application.VBE.ActiveVBProject.References
    If ref.Description Like refDescrip Then
        RefExists = True
        Exit Function
    End If
Next
RefExists = bExists
End Function

或者

使用早期绑定(带有参考)在计算机上开发代码,然后在分发之前,将所有特定于Outlook的声明(例如,As MailItemAs Outlook.Application等)更改为通用As Object类型.您的代码仍将执行,并且不需要引用.

Develop the code on your machine using early binding (with the reference), then before you distribute, change all of the outlook-specific declarations (e.g., As MailItem, As Outlook.Application, etc.) to generic As Object type. Your code will still execute and will not require the references.

使用后期绑定,只需要将适当的库放在用户的计算机上即可.这通常不是问题,因为您没有使用任何类型的自定义类型库或dll,而是使用了标准的Office组件库,而该组件库不会包含在常规Windows安装中.

With late-binding all that is required is that the appropriate libraries are on the users' machines. This is usually not a problem since you're not using any sort of custom type library or dll, but a standard Office component library that would not be part of the normal windows install.

立即想到的唯一其他区别是您不能在赋值或声明中使用New关键字,例如:

The only other difference that immediately comes to mind is that you can't use the New keyword on assignment or declaration, e.g.,:

Dim olApp as New Outlook.Application

或者:

Dim olApp as Outlook.Application
Set olApp = New Outlook.Application

相反,您必须使用CreateObject方法:

Instead, you have to use the CreateObject method:

Dim olApp as Object 'Outlook.Application object
Set olApp = CreateObject("Outlook.Application")

这篇关于如何从Excel中选择对MS Outlook用户版本的“引用"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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