GetObject(,“Outlook.Application”)无法使用Outlook打开

时间:2015-06-17 12:11:35

标签: excel vba outlook

我在Excel文件中使用以下代码,可供多人访问和使用。在继续执行其余代码之前,摘录会检查Outlook是否已打开。

Dim oOutlook As Object

'Checks to see if Outlook is open
On Error Resume Next
Set oOutlook = GetObject(, "Outlook.Application")
On Error GoTo 0

If oOutlook Is Nothing Then
    MsgBox "Outlook is not open."
    Exit Sub
End If
Set oOutlook = Nothing

该代码似乎适用于除一个人/计算机之外的所有人。对于这个人/计算机,即使Outlook打开,代码行     Set oOutlook = GetObject(, "Outlook.Application") 似乎没有意识到它是开放的。我检查了常见的事情:确保VBA引用设置正确,安全设置似乎与其他人一样。

任何建议都将不胜感激。

4 个答案:

答案 0 :(得分:0)

根据我的经验,我用它来避免这个问题:

On Error Resume Next
Set oWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
    Set oWord = CreateObject("Word.Application")
End If
On Error GoTo 0

但实际上我不知道问题来自哪里......有时我只是为我而过!

答案 1 :(得分:0)

在具有UAC的系统上,两个应用程序都应在相同的安全上下文中运行。例如,如果其中一个应用程序以管理员权限运行,则应使用管理员权限运行另一个应用程序以访问正在运行的实例。

您可能会发现this tutorial文章有用。

答案 2 :(得分:0)

我曾经不得不这样做,因为当第一个getobject失败时出现错误,所以我开始重复3次或更多次,并且再也没有出现过问题:

on error resume next
Dim AutoCAD As Object
Dim Thisdrawing As Object
Dim activedocument As Object
Dim acadapplication As Object
Dim acadapp As Object
Set acadapp = GetObject(, "autocad.application.14")
Set acadapp = GetObject(, "autocad.application.15")
Set acadapp = GetObject(, "autocad.application.16")
Set acadapp = GetObject(, "autocad.application.17")
Set acadapp = GetObject(, "autocad.application.18")
Set acadapp = GetObject(, "autocad.application.19")
Set acadapp = GetObject(, "autocad.application.20")
Set acadapp = GetObject(, "autocad.application.21")
Set acadapp = GetObject(, "autocad.application.22")
Set acadapp = GetObject(, "autocad.application.23")
Set acadapp = GetObject(, "autocad.application.24")
Set acadapp = GetObject(, "autocad.application.25")
Set acadapp = GetObject(, "autocad.application.26")
Set acadapp = GetObject(, "autocad.application.27")
Set acadapp = GetObject(, "autocad.application.28")
Set acadapp = GetObject(, "autocad.application.29")
Set acadapp = GetObject(, "autocad.application.30")

Set acadapp = GetObject(, "autocad.application")
Set acadapp = GetObject(, "autocad.application")
Set acadapp = GetObject(, "autocad.application")

为了防止有更新的版本我尝试了一次,然后重复通常多次。再也没有失败过。

答案 3 :(得分:0)

Option Explicit
Function CreateOutlook() As Object
    Dim Outlook As Object
    On Error Resume Next
        Set Outlook = GetObject(, "Outlook.Application")
    On Error GoTo 0
    If Outlook Is Nothing Then Shell "Outlook"
    On Error Resume Next
        Do While Outlook Is Nothing
            Set Outlook = GetObject(, "Outlook.Application")
        Loop
    On Error GoTo 0
    Set Outlook = CreateObject("Outlook.Application")
    Outlook.ActiveWindow.WindowState = 1
    Set CreateOutlook = Outlook
    Set Outlook = Nothing
End Function

Sub Test()
    Dim Outlook As Object
    Set Outlook = CreateOutlook()
    ... 'Other operations
End Sub

一切都很完美。

编辑:

GetObject有时无法正确返回对象。我使用GetObject检查是否打开了应用程序。打开应用程序时,我创建一个正常工作的对象。