John Clingeleffer
asked on
CreateObject does not create an Outlook Instance Under Access 2010
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.Appl ication")
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
'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.Appl
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
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.
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.
YOu haven't created it i.e. you need:
Set oItem= objOL.CreateItem(0) ' Const olMailItem
Chris
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
ASKER
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
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
ASKER
Set oItem= objOL.CreateItem(0) ' Const olMailItem
gives the same error as
Set oItem= objOL.CreateItem(olMailIte m)
"Application-defined or Object-defined error"
gives the same error as
Set oItem= objOL.CreateItem(olMailIte
"Application-defined or Object-defined error"
<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.
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.
ASKER
Moved everything to Documents and same error
ASKER
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?
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?
ASKER
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.
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.
ASKER
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.
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.
>>> 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
of course but in theory you do so, but modded to use string blank below
Chris
If attachmentPath <> "" then
.attachments.Add attachmentPath
End if
ASKER
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.
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.
In what way does it fall over?
Chris
Chris
ASKER
With no attachment fails on the .Send line with error number 287 "Application-defined or object-defined error"
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
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
ASKER
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
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
So you do not get an error message in a msgbox saying ""objOL is still nothing"" when outlook is closed in 07/10?
Chris
Chris
ASKER
No
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.
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.
Lets try some late binding ... the errors should be at compile pre run but it does no harm to double check.
Chris
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
Have just looked over, I think I missed this approach
Chris
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
ASKER
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.Appl ication")
.
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.
Set objOL = CreateObject("Outlook.Appl
.
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.
If path is a string then try:
If attachmentPath <> "" then
If attachmentPath <> "" then
ASKER
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.
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.
I need to review the thread so i'll try later today.
Chris
Chris
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That did it!
It needed the objol.session.logon
Thank you
It needed the objol.session.logon
Thank you
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
Chris
ASKER
Yes, it's a bit weird that no attachment needs the .logon but with an attachment it doesn't need the .logon
Thanks again.
Thanks again.
Nice to know. This seems to come up every once in a while.
ASKER
'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.Appl
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