ActiveX component can't create object. VBA Sending Email

russ0670

New member
Local time
Today, 16:34
Joined
Jun 27, 2015
Messages
8
I am using the following code but when i run it produces the following error.

Run-time error '429':

ActiveX component can't create object.

then on debug it highlights the line
Set objOutlook = CreateObject("Outlook.Application")

I have searched online and already tried a suggestion to ensure that mictosoft outlook 14.0 object library is selected in the VBA references.

Anyone have an idea?

Code:
Dim objOutlook As Object
Dim objMailItem As Object
Const olMailItem As Integer = 0
Set objOutlook = CreateObject("Outlook.Application")
Set objMailItem = objOutlook.CreateItem(olMailItem)

 'set the object from template
Set objMailItem = objOutlook.CreateItemFromTemplate("C:\FastFile\System 3.0\3.0\BLANKTEMP.oft")


strEmail = "test@testing.com"
strSubject = "Test"
strBody = "Test the Outlook Object"
strPathAttach = "C:\FastFile\System 3.0\sampleatt.pdf"


On Error GoTo err_Error_handler

'set receipient
objMailItem.To = strEmail
'set subject
objMailItem.Subject = strSubject
'set body content
objMailItem.Body = strBody
'Check path and file, then set Attachment

If Len(Dir(strPathAttach)) Then
   With objMailItem.Attachments
      'use parenthisis around the path
      .Add (strPathAttach)
  
   End With
End If

'sending email
objMailItem.Send


 Set objOutlook = Nothing
 Set objMailItem = Nothing


exit_Error_handler:
 On Error Resume Next

 Set objOutlook = Nothing
 Set objMailItem = Nothing
 
 Exit Sub
 

err_Error_handler:
 Select Case Err.Number
  'trap error 287
  Case 287
   MsgBox "Canceled by user.", vbInformation
  Case Else
   MsgBox "Error " & Err.Number & " " & Err.Description
 End Select

Resume exit_Error_handler
 
SOLVED

Found it was because Access was open as administrator and outlook was not. Set both to normal and fires perfectly.

Also works if both set to open as administrator. go figure
 
To add to this you first attempt to GetObject() if Outlook is already running, and if it fails, CreateObject()

Aircode:
Code:
on error resume next
getobject()

if err.number <> 0 then
    setobject()
    err.clear
end if
on error go to err_label
 
vbaInet: can you explain the point of using get before create?

Create knows that only one instance of Outlook can run, so it gets it, if already running. For Word or Excel or other multi-instance applications there is of course a difference between get and create, in that "create" is "dumb" and creates a new instance on each call. That does not happen for Outlook.

So what have I missed in the above?
 
I think we've had this discussion at some point. CreateObject() is not the clever function that decides whether a new Outlook instance is created or not, it's the definition of the Outlook class that defines it as a Singleton. This is only a fail safe measure so why rely on it? And even if the Outlook Class is a singleton I still like to explicitly attempt to get the object (if it's open) and do otherwise if it isn't. It's simply a good practice to adopt.
 

Users who are viewing this thread

Back
Top Bottom