vba 我需要为 Excel 中对 MS Outlook 的正确“参考”进行编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24630378/
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
I need to code for the correct 'Reference' to MS Outlook from Excel
提问by M. H
I want to run some code in Excel
, that talks to Outlook
. On my machine, I can just select the right reference from Tools->References
in the VBE.
我想在 中运行一些代码Excel
,与Outlook
. 在我的机器上,我可以从Tools->References
VBE 中选择正确的参考。
But I want my code to run for other users on their machines too and they will all have different versions of Outlook and Excel,
但我希望我的代码也能在他们的机器上为其他用户运行,他们都有不同版本的 Outlook 和 Excel,
Is there a tidy way I can make the code select the right reference to MS Outlook, or tell me if Outlook isn't installed, etc?
有没有一种整洁的方法可以让代码选择对 MS Outlook 的正确引用,或者告诉我是否未安装 Outlook,等等?
Thanks
谢谢
回答by David Zemens
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.
我使用这样的功能,它应该适用于 Outlook 2010。如果您使用的是不同版本的 Office,您可能需要更改路径/参数,或者如果您必须处理多个版本的 Office,那么您将需要一些额外的处理版本控制的逻辑,但这是它的基础知识。
This subroutine adds the reference if it doesn't already exist
如果引用不存在,此子例程会添加引用
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
This function checks to see if the reference exists (or not)
此函数检查引用是否存在(或不存在)
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
Alternatively
或者
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.
发展你的机器上使用早期绑定(与基准)的代码,然后分发之前,所有Outlook特定的声明(例如,改变As MailItem
,As Outlook.Application
等等),以通用As Object
型。您的代码仍将执行并且不需要引用。
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.
对于后期绑定,所需的只是适当的库位于用户的机器上。这通常不是问题,因为您没有使用任何类型的自定义类型库或 dll,而是一个标准的 Office 组件库,它不会成为正常 Windows 安装的一部分。
The only other difference that immediately comes to mind is that you can't use the New
keyword on assignment or declaration, e.g.,:
立即想到的唯一其他区别是您不能New
在赋值或声明中使用关键字,例如:
Dim olApp as New Outlook.Application
Or:
或者:
Dim olApp as Outlook.Application
Set olApp = New Outlook.Application
Instead, you have to use the CreateObject
method:
相反,您必须使用以下CreateObject
方法:
Dim olApp as Object 'Outlook.Application object
Set olApp = CreateObject("Outlook.Application")
回答by Alexandr Panchenko
Small append for first sub:
第一个子项的小附加:
Sub AddRefToOutlook()
'This subroutine adds the reference if it doesn't already exist
Const outlookRef As String = "C:\Program Files (x86)\Microsoft Office\Office14\MSOUTL.OLB"
If Not RefExists(outlookRef, "Microsoft Outlook " & CLng(Split(Application.Version, ".")(0)) & ".0 Object Library") Then
Application.VBE.ActiveVBProject.References.AddFromFile ("MSOUTL.OLB")
End If
End Sub