vba 从 Access 打开 Outlook 的后期绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19184650/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:40:01  来源:igfitidea点击:

Late Binding to Open Outlook from Access

vbams-accessoutlook

提问by Grant

I'm trying to open the Outlook application from Access VBA when the switchboard loads. I've opened task manager and I can see an instance of Outlook appear for about 5 seconds then close, but I can't get the explorer window to open. I've been trying to piece together code from VBA: Determining whether an existing Outlook instance is openand other sources, but it's just not working. Any ideas?

我正在尝试在总机加载时从 Access VBA 打开 Outlook 应用程序。我打开了任务管理器,我可以看到 Outlook 的一个实例出现了大约 5 秒钟然后关闭,但我无法打开资源管理器窗口。我一直在尝试将VBA 中的代码拼凑起来:确定现有 Outlook 实例是否是开放的和其他来源,但它不起作用。有任何想法吗?

And I would like to stick with late bindings so I don't have to worry about object libraries if someone opens with XP.

而且我想坚持使用后期绑定,这样如果有人用 XP 打开,我就不必担心对象库。

Function OpenEmail()

Dim olApp As Object ' Outlook.Application
Dim olFolderInbox As Object
Dim objExplorer As Object

On Error Resume Next

Set olApp = GetObject(, "Outlook.Application")

If olApp Is Nothing Then
    MsgBox "Outlook is not Open"
    Set olApp = CreateObject("Outlook.Application")
End If

Set objExplorer = CreateObject("Outlook.MAPIFolder")
Set objExplorer = olApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox)

objExplorer.Activate

'Set olApp = Nothing

End Function

回答by Siddharth Rout

Outlook is the only MS Office application where GetObjectdoes the same thing as CreateObject. Unlike other MS Office application, CreateObjectdoesn't create multiple instances of Outlook.

Outlook 是唯一GetObject一个与CreateObject. 与其他 MS Office 应用程序不同,CreateObject它不会创建多个 Outlook 实例。

Also olFolderInboxis an outlook constant. You will have to define it in ACCESS.

也是olFolderInbox一个展望常数。您必须在 ACCESS 中定义它。

Try this

尝试这个

Const olFolderInbox As Long = 6

Sub Sample()
    Dim olApp As Object
    Dim objNS As Object
    Dim olFolder As Object

    Set olApp = CreateObject("Outlook.Application")
    Set objNS = olApp.GetNamespace("MAPI")
    Set olFolder = objNS.GetDefaultFolder(olFolderInbox)

    With olFolder
        '~~> Do what you want
    End With
End Sub