qagofnz
qagofnz

Reputation: 51

GetObject(, "Outlook.Application") not working with Outlook open

I am using the below code in an Excel file which can be accessed and used by multiple people. The extract checks to see if Outlook is open before proceeding 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 apart from one person/computer. For this one person/computer, even with Outlook open, the line of code Set oOutlook = GetObject(, "Outlook.Application") does not seem recognise that it is open. I've checked the usual things: made sure the VBA references are set up correctly, security settings seem to be the same as everyone else.

Any suggestions would be greatly appreciated.

Upvotes: 5

Views: 13380

Answers (4)

Riccardo La Marca
Riccardo La Marca

Reputation: 895

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

All works perfectly.

Edit:

GetObject sometimes not return correctly the object. I use GetObject for check if application is opened. When the application is opened i create a object that works correctly.

Upvotes: 1

SkyscraperFX
SkyscraperFX

Reputation: 1

I used to have to do this because that error came up when the first getobject failed, so I started repeating it 3 or more times, and never had an issue again:

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

Just in case there were newer versions I tried them all once, then repeated the usual a number of times. Never had a fail again.

Upvotes: 0

Eugene Astafiev
Eugene Astafiev

Reputation: 49453

On systems with UAC both applications should be run under the same security context. For example, if one of the applications is run with admin privileges, another should be run with admin privileges as well to access a running instance.

You may find the How to automate Outlook from another program article helpful.

Upvotes: 0

R3uK
R3uK

Reputation: 14547

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 clue where the issue come from... It just pass by for me sometimes!

Upvotes: 2

Related Questions