从Access访问后期绑定到打开Outlook

时间:2013-10-04 15:06:51

标签: vba ms-access outlook

我正在尝试在交换机加载时从Access VBA打开Outlook应用程序。我打开了任务管理器,我可以看到一个Outlook实例出现约5秒然后关闭,但我无法打开资源管理器窗口。我一直试图将来自VBA: Determining whether an existing Outlook instance is open和其他来源的代码拼凑在一起,但它只是不起作用。有什么想法吗?

我想坚持使用后期绑定,所以如果有人打开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

1 个答案:

答案 0 :(得分:4)

Outlook是GetObjectCreateObject完全相同的唯一MS Office应用程序。与其他MS Office应用程序不同,CreateObject不会创建多个Outlook实例。

同样olFolderInbox是展望常数。您必须在ACCESS中定义它。

试试这个

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