We help IT Professionals succeed at work.

CreateObject does not create an Outlook Instance Under Access 2010

John Clingeleffer
John Clingeleffer used Ask the Experts™
on
I have an Access 2003 application that uses the Outlook Object Model to send out e-mails. The following code runs fine under Access 2003, but does not open an instance of Outlook 2010 when executed under Windows 7/Access 2010. Err returned from Set objOL = GetObject(, "Outlook.Application") is 429

'code to start outlook
 Dim outlookForceStarted As Boolean
 Dim objOL As Outlook.Application
 Dim oItem As Outlook.MailItem
 outlookForceStarted = False

'set error handling to resume next as GetObject will generate an error if
'Outlook is not running
On Error Resume Next

'see if Outlook is running
errMsg = "Error checking if Microsoft Outlook is running."
Set objOL = GetObject(, "Outlook.Application")
'non zero error indicates Outlook is not running so start it
If Err <> 0 Then
    'Outlook wasn't running, so start it
     errMsg = "Error starting Microsoft Outlook."
     Set objOL = CreateObject("Outlook.Application")
     outlookForceStarted = True
End If

'change following line to reflect the error handling routine of the function/sub
'that this code is being pasted into
On Error GoTo PrintErrorBit
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Further to initial post, the code crashes at the .Send line. See below
'code to start outlook
Dim outlookForceStarted As Boolean
Dim objOL As Outlook.Application
Dim oItem As Outlook.MailItem
outlookForceStarted = False

'set error handling to resume next as GetObject will generate an error if
'Outlook is not running
On Error Resume Next

'see if Outlook is running
errMsg = "Error checking if Microsoft Outlook is running."
Set objOL = GetObject(, "Outlook.Application")
'non zero error indicates Outlook is not running so start it
If Err <> 0 Then
'Outlook wasn't running, so start it
errMsg = "Error starting Microsoft Outlook."
Set objOL = CreateObject("Outlook.Application")
outlookForceStarted = True
End If

'change following line to reflect the error handling routine of the function/sub
'that this code is being pasted into
On Error GoTo PrintErrorBit

With oItem
errMsg = "Error populating Microsoft Outlook mail item components."
.To = Trim(emAddress)
.Subject = emSubject
.Body = MergeBody(PIN, msgBody)
If Not Isnull(attachmentPath) then
.attachments.Add attachmentPath
End if
errMsg = "Error sending Microsoft Outlook mail item."
.Send 'this line crashes
End With
Scott McDaniel (EE MVE )Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014

Commented:
First things: Have you compiled your code on the Win7 box?

Where is the app installed on the Win7 box? It should be in one of the Users folders, not in C:/Program Files

Is the database location added as a Trusted Location?

Can you verify whether the Outlook object has actually been created? To do this, add a Msgbox somewhere AFTER the CreateObject method:

Msgbox objOL Is Nothing

And see what that returns.

Chris BottomleySoftware Quality Lead Engineer
Top Expert 2011

Commented:
YOu haven't created it i.e. you need:

    Set oItem= objOL.CreateItem(0) ' Const olMailItem

Chris
Dim outlookForceStarted As Boolean
Dim objOL As Outlook.Application
Dim oItem As Outlook.MailItem
outlookForceStarted = False

'set error handling to resume next as GetObject will generate an error if
'Outlook is not running 
On Error Resume Next

'see if Outlook is running
errMsg = "Error checking if Microsoft Outlook is running."
Set objOL = GetObject(, "Outlook.Application")
'non zero error indicates Outlook is not running so start it
If Err <> 0 Then
'Outlook wasn't running, so start it
errMsg = "Error starting Microsoft Outlook."
Set objOL = CreateObject("Outlook.Application")
outlookForceStarted = True
End If

'change following line to reflect the error handling routine of the function/sub
'that this code is being pasted into
On Error GoTo PrintErrorBit 
Set oItem= objOL.CreateItem(0) ' Const olMailItem
With oItem
errMsg = "Error populating Microsoft Outlook mail item components."
.To = Trim(emAddress)
.Subject = emSubject
.Body = MergeBody(PIN, msgBody)
If Not Isnull(attachmentPath) then
.attachments.Add attachmentPath
End if
errMsg = "Error sending Microsoft Outlook mail item."
.Send 'this line crashes
End With

Open in new window

Author

Commented:
First things: Have you compiled your code on the Win7 box?
Yes

Where is the app installed on the Win7 box? It should be in one of the Users folders, not in C:/Program Files
App installed at c:\SEMP

Is the database location added as a Trusted Location?

Yes

Can you verify whether the Outlook object has actually been created? To do this, add a Msgbox somewhere AFTER the CreateObject method:

Msgbox objOL Is Nothing

And see what that returns.

Displays False

Author

Commented:
Set oItem= objOL.CreateItem(0) ' Const olMailItem
gives the same error as
Set oItem= objOL.CreateItem(olMailItem)
"Application-defined or Object-defined error"
Scott McDaniel (EE MVE )Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014

Commented:
<App installed at c:\SEMP>

Just for kicks, try installing the db in the Documents folder. The root drive of a Win7 box is a protected directory, and as such you may run into issues trying to create anything in that directory.

Author

Commented:
Moved everything to Documents and same error

Author

Commented:
Further update on initial post. If I change (changes in bold)
With oItem
errMsg = "Error populating Microsoft Outlook mail item components."
.To = Trim(emAddress)
.Subject = emSubject
.Body = MergeBody(PIN, msgBody)
If Not Isnull(attachmentPath) then
.attachments.Add attachmentPath
End if
errMsg = "Error sending Microsoft Outlook mail item."
.Send
End With
to
With oItem
errMsg = "Error populating Microsoft Outlook mail item components."
.To = Trim(emAddress)
.Subject = emSubject
.Body = MergeBody(PIN, msgBody)
If Not Isnull(attachmentPath) then
.attachments.Add attachmentPath
End if
errMsg = "Error sending Microsoft Outlook mail item."
.Save
.Display
MsgBox "Wait"
End With
the message is displayed correctl in Outlook so Outlook is being started. Am I missing an Outlook send security setting?

Author

Commented:
Further update:

Key bit of code is:
Set oItem= objOL.CreateItem(0) ' Const olMailItem
With oItem
errMsg = "Error populating Microsoft Outlook mail item components."
.To = Trim(emAddress)
.Subject = emSubject
.Body = MergeBody(PIN, msgBody)
If Not Isnull(attachmentPath) then
    .attachments.Add attachmentPath
End if
errMsg = "Error sending Microsoft Outlook mail item."
.Send
End With

Code run fine under Windows XP/Access 2003/Outlook 2003 whether or not Outlook is open and ehether or not there is .
Under Windows Vista/Access 2007/Outlook 2007 and Windows 7/Access 2010/Outlook 2010 the code runs fine if there is an attachment, fails if there is no attachment.  Should the something done with .Attachments if there is no attachment.

Author

Commented:
Previous should have read:
Further update:

Key bit of code is:
Set oItem= objOL.CreateItem(0) ' Const olMailItem
With oItem
errMsg = "Error populating Microsoft Outlook mail item components."
.To = Trim(emAddress)
.Subject = emSubject
.Body = MergeBody(PIN, msgBody)
If Not Isnull(attachmentPath) then
    .attachments.Add attachmentPath
End if
errMsg = "Error sending Microsoft Outlook mail item."
.Send
End With

Code run fine under Windows XP/Access 2003/Outlook 2003 whether or not Outlook is open and whether or not there is an attachment.
Under Windows Vista/Access 2007/Outlook 2007 and Windows 7/Access 2010/Outlook 2010 the code runs fine with or without an attachment if Outlook is running. If Outlook is not running, code works fine  if there is an attachment, fails if there is no attachment.  

Should the something done with .Attachments if there is no attachment.
Chris BottomleySoftware Quality Lead Engineer
Top Expert 2011

Commented:
>>> Should the something done with .Attachments if there is no attachment.

of course but in theory you do so, but modded to use string blank below

Chris
If attachmentPath <> "" then
    .attachments.Add attachmentPath
End if

Open in new window

Author

Commented:
Hello Chris,

The routine is always called with either Null or a string, can't have a zero length string. Since last posting have tried it with all the attachment stuff commented out. I.E.

Set oItem= objOL.CreateItem(0) ' Const olMailItem
With oItem
errMsg = "Error populating Microsoft Outlook mail item components."
.To = Trim(emAddress)
.Subject = emSubject
.Body = MergeBody(PIN, msgBody)
'If Not Isnull(attachmentPath) then
'    .attachments.Add attachmentPath
'End if
errMsg = "Error sending Microsoft Outlook mail item."
.Send
End With

With no attachment code being called at all runs fine on WinXP/Office 2003 but falls over on Win7/Office 2010 if Outlook is not initially running. Runs fine if outlook is running.
Chris BottomleySoftware Quality Lead Engineer
Top Expert 2011

Commented:
In what way does it fall over?

Chris

Author

Commented:
With no attachment fails on the .Send line with error number 287 "Application-defined or object-defined error"
Chris BottomleySoftware Quality Lead Engineer
Top Expert 2011

Commented:
Changed method but I still expect it to fail, do you get a message with 7/2010 to indicate objol is nothing first or does it fail without presenting that message?

Chris
Dim outlookForceStarted As Boolean
Dim objOL As Outlook.Application
Dim oItem As Outlook.MailItem
outlookForceStarted = False

'set error handling to resume next as GetObject will generate an error if
'Outlook is not running 
On Error Resume Next

'see if Outlook is running
errMsg = "Error checking if Microsoft Outlook is running."
Set objOL = GetObject(, "Outlook.Application")
'non zero error indicates Outlook is not running so start it
'If Err <> 0 Then
if objOL is nothing then
'Outlook wasn't running, so start it
errMsg = "Error starting Microsoft Outlook."
Set objOL = CreateObject("Outlook.Application")
outlookForceStarted = True
End If
if objOL is nothing then
    msgbox "objOL is still nothing"
end if
'change following line to reflect the error handling routine of the function/sub
'that this code is being pasted into
On Error GoTo PrintErrorBit 
Set oItem= objOL.CreateItem(0) ' Const olMailItem
With oItem
errMsg = "Error populating Microsoft Outlook mail item components."
.To = Trim(emAddress)
.Subject = emSubject
.Body = MergeBody(PIN, msgBody)
If Not Isnull(attachmentPath) then
.attachments.Add attachmentPath
End if
errMsg = "Error sending Microsoft Outlook mail item."
.Send 'this line crashes
End With

Open in new window

Author

Commented:
Fails on the .Send line with error 287 "Application-defined or object-defined error"
Only fails on Access 2007 or Access 2010 and only fails if:
1. There is no attachment - attachmentPath is Null
2. Outlook was not open when prodecure is run
Chris BottomleySoftware Quality Lead Engineer
Top Expert 2011

Commented:
So you do not get an error message in a msgbox saying ""objOL is still nothing"" when outlook is closed in 07/10?

Chris

Author

Commented:
No
Scott McDaniel (EE MVE )Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014

Commented:
Have you tried this with UAC temporarily turned OFF? May be a permissions/security issue, but it's hard to say.

Can you upload the database, or at least a database with that code? I can run it in my vmWare Win7/2010 install and see what happens.
Chris BottomleySoftware Quality Lead Engineer
Top Expert 2011

Commented:
Lets try some late binding ... the errors should be at compile pre run but it does no harm to double check.

Chris
Dim outlookForceStarted As Boolean
Dim objOL As object
Dim oItem As object
dim errmsg as string
outlookForceStarted = False

'set error handling to resume next as GetObject will generate an error if
'Outlook is not running 
On Error Resume Next

'see if Outlook is running
errMsg = "Error checking if Microsoft Outlook is running."
Set objOL = GetObject(, "Outlook.Application")
'non zero error indicates Outlook is not running so start it
'If Err <> 0 Then
if objOL is nothing then
'Outlook wasn't running, so start it
errMsg = "Error starting Microsoft Outlook."
Set objOL = CreateObject("Outlook.Application")
outlookForceStarted = True
End If
if objOL is nothing then
    msgbox "objOL is still nothing"
end if
'change following line to reflect the error handling routine of the function/sub
'that this code is being pasted into
On Error GoTo PrintErrorBit 
Set oItem= objOL.CreateItem(0) ' Const olMailItem
With oItem
errMsg = "Error populating Microsoft Outlook mail item components."
.To = Trim(emAddress)
.Subject = emSubject
.Body = MergeBody(PIN, msgBody)
If Not Isnull(attachmentPath) then
.attachments.Add attachmentPath
End if
errMsg = "Error sending Microsoft Outlook mail item."
.Send 'this line crashes
End With

Open in new window

Chris BottomleySoftware Quality Lead Engineer
Top Expert 2011

Commented:
Have just looked over, I think I missed this approach

Chris


Dim outlookForceStarted As Boolean
Dim objOL As Object
Dim oItem As Object

    Set objOL = CreateObject("Outlook.Application")
    Set oItem = objOL.CreateItem(0) ' Const olMailItem
    With oItem
        .To = Trim(emAddress)
        .Subject = emSubject
        .Body = MergeBody(PIN, msgBody)
        If Not IsNull(attachmentPath) Then
            .attachments.Add attachmentPath
        End If
        .Send 'this line crashes
    End With

Open in new window

Author

Commented:
Have done further testing. Rather than post all code I will only post the problem bit. In summary, this is a function that is called from an Access application and uses the Outlook Object Model. At the start, the function checks if Outlook is running, if it is not, it starts Outlook using
Set objOL = CreateObject("Outlook.Application")
.
There is no problem with this process. The problem comes with the following code:
With oItem
    .To = Trim(EMailWhoTo)
    .Subject = MessageSubject
    .Body = msgBody
    If Not IsNull(primaryAttachment) And primaryAttachment <> "" Then .Attachments.Add primaryAttachment
    .Send
End With

On a computer running Win XP with Office 2003, this code runs fine with or without an attachment, regardless of whether Outlook is already running or is started by the function.

On a computer running Vista with Office 2007 or Win 7 with Office 2010 this code runs fine with or without an attachment if Outlook is already running.

However, if Outlook is started by the function, the code runs fine if there is an attachment, but crashes on the .Send line if there is no attachment or if the
 If Not IsNull(primaryAttachment) And primaryAttachment <> "" Then .Attachments.Add primaryAttachment
line is commented out or removed.
The error is 287 Application-defined or object-defined error.
Chris BottomleySoftware Quality Lead Engineer
Top Expert 2011

Commented:
If path is a string then try:

If attachmentPath <> "" then

Author

Commented:
Tried that. But just changing code to
With oItem
    .To = Trim(EMailWhoTo)
    .Subject = MessageSubject
    .Body = msgBody
    .Send
End With

Crashes on the .Send line if if Outlook is started by the function, but runs fine if Outlook is already running.
Chris BottomleySoftware Quality Lead Engineer
Top Expert 2011

Commented:
I need to review the thread so i'll try later today.

Chris
Software Quality Lead Engineer
Top Expert 2011
Commented:
One thought:

to try and logon to the default session:

Chris
Dim outlookForceStarted As Boolean
Dim objOL As object
Dim oItem As object
dim errmsg as string
outlookForceStarted = False

'set error handling to resume next as GetObject will generate an error if
'Outlook is not running 
On Error Resume Next

'see if Outlook is running
errMsg = "Error checking if Microsoft Outlook is running."
Set objOL = GetObject(, "Outlook.Application")
'non zero error indicates Outlook is not running so start it
'If Err <> 0 Then
if objOL is nothing then
'Outlook wasn't running, so start it
errMsg = "Error starting Microsoft Outlook."
Set objOL = CreateObject("Outlook.Application")
objol.session.logon
outlookForceStarted = True
End If
if objOL is nothing then
    msgbox "objOL is still nothing"
end if
'change following line to reflect the error handling routine of the function/sub
'that this code is being pasted into
On Error GoTo PrintErrorBit 
Set oItem= objOL.CreateItem(0) ' Const olMailItem
With oItem
errMsg = "Error populating Microsoft Outlook mail item components."
.To = Trim(emAddress)
.Subject = emSubject
.Body = MergeBody(PIN, msgBody)
If Not Isnull(attachmentPath) then
.attachments.Add attachmentPath
End if
errMsg = "Error sending Microsoft Outlook mail item."
.Send 'this line crashes
End With

Open in new window

Author

Commented:
That did it!
 It needed the objol.session.logon

Thank you
Chris BottomleySoftware Quality Lead Engineer
Top Expert 2011

Commented:
Very sorry it took so long to get there, I have no idea why the combination should be so different but I am very glad we got there.

Chris

Author

Commented:
Yes, it's a bit weird that no attachment needs the .logon but with an attachment it doesn't need the .logon

Thanks again.
Scott McDaniel (EE MVE )Infotrakker Software
Most Valuable Expert 2012
Top Expert 2014

Commented:
Nice to know. This seems to come up every once in a while.