ActiveX component can't create object. VBA Sending Email (1 Viewer)

russ0670

New member
Local time
Today, 16:22
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
 

russ0670

New member
Local time
Today, 16:22
Joined
Jun 27, 2015
Messages
8
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
 

vbaInet

AWF VIP
Local time
Today, 14:22
Joined
Jan 22, 2010
Messages
26,374
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
 

spikepl

Eledittingent Beliped
Local time
Today, 15:22
Joined
Nov 3, 2010
Messages
6,142
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?
 

vbaInet

AWF VIP
Local time
Today, 14:22
Joined
Jan 22, 2010
Messages
26,374
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

Top Bottom