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

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

问题描述

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

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

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

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.?

推荐答案

我使用了这样的函数,它应该适用于 Outlook 2010.如果您使用的是不同版本的 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 OfficeOffice14MSOUTL.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天全站免登陆