0
votes

I have this VBA code use to send emails in my Access Database, and I also Referenced the Microsoft Outlook 14.0 Object Library.

Everything works perfectly when I run the code manually. However, Access fails when I run the Macro from Windows Task Schedular. I get this message:

Runtime error '429' ActiveX component can't create object

In the Actions tab in Task Scheduler I am referencing the Macro like this: "C:\Program Files\Microsoft Office\Office14\MSACCESS.EXE" "C:\mydata\Database\mydatabase.accdb" /X RunMyMacro

The error seem to be pointing to this code as indicated in the screenshot below.

Code:

Dim strEmailBody As String

Sub emailNotification()
Dim oFolder As Outlook.MAPIFolder
Dim oItem As Outlook.MailItem
Dim oOutlook As New Outlook.Application

Dim MoOutlook As Outlook.NameSpace
Set MoOutlook = oOutlook.GetNamespace("MAPI")
Set oFolder = MoOutlook.GetDefaultFolder(olFolderOutbox)
Set oItem = oFolder.Items.Add(olMailItem)
With oItem
.Recipients.Add ("[email protected]")
.Subject = "my notification"
.Body = strEmailBody
.Importance = olImportanceHigh
.Send
End With
Set oFolder = Nothing
End Sub

Screenshot:

enter image description here

1
can you run "C:\Program Files\Microsoft Office\Office14\MSACCESS.EXE" "C:\mydata\Database\mydatabase.accdb" /X RunMyMacro in the command line? - HelloW
Yes it works when I type or paste the above in the command line. Is this a problem with Task Scheduler? - Asynchronous
have you tried editing the permissions? superuser.com/questions/402070/… - HelloW

1 Answers

0
votes

Put the following code in a batch file and run the batch file from Windows Scheduler.

"C:\Program Files\Microsoft Office\Office14\MSACCESS.EXE" "C:\mydata\Database\mydatabase.accdb" /X RunMyMacro