GetObject (, "Outlook.Application") does not work with Outlook open

I am using the below code in an excel file that multiple people can access and use. The extract checks to see if Outlook is open before continuing with the rest of the code.

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

      

The code seems to work for everyone but one person / computer. For this person / computer, even with Outlook open, the line of code    Set oOutlook = GetObject(, "Outlook.Application")

doesn't look like it's open. I've checked the usual stuff: make sure the VBA links are set up correctly, the security settings seem to be the same with everyone.

Any suggestions are greatly appreciated.

+4


source to share


4 answers


In my experience, I use this to avoid this problem:

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

      



But actually I have no idea where this question comes from ... It sometimes passes me by!

0


source


On systems with UAC, both applications must run under the same security context. For example, if one of the applications runs with administrator rights, the other must run with administrator rights and also to access the running instance.



You can find How to Automate Outlook from Another Program . Helpful.

0


source


I had to do this because this error occurred when the first getobject failed, so I started repeating it 3 or more times and had no problem anymore:

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")

      

Newer versions appeared just in case, I tried them all once, and then repeated the usual time. Never again failed.

0


source


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

      

Everything works perfectly.

Edit:

GetObject sometimes returns an object incorrectly. I am using GetObject to check if an application is open. When the app opens, I create an object that works correctly.

0


source







All Articles