I am trying on automating a task wherein an outlook email will be sent automatically based on the excel file's values. However, send to email option is not working when I use task scheduler.
It works fine when I try to manually launch the VBScript File or run the macro from excel. During debug, Set MyOutlook = New Outlook.Application is highlighted
Dim MyOutlook As Outlook.Application
Set MyOutlook = New Outlook.Application
Dim MyMail As Object
Set MyMail = MyOutlook.CreateItem(olMailItem)
MyMail.To = "[email protected]"
MyMail.CC = "[email protected]"
MyMail.Subject = "Sample Alert"
MyMail.Body = "Hello Team," & vbNewLine & _
" " & vbNewLine & _
"Kindly check this Alert" & vbNewLine & _
" " & vbNewLine & _
"Thanks!" & vbNewLine & _
" " & vbNewLine & _
"Surname"
First and foremost, Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution. Read more about that in the Considerations for server-side Automation of Office article.
You may consider using EWS, Graph API instead if you are dealing with Exchange.
Also make sure that Outlook is not run at the time of execution of your script. Outlook is a singleton which means only one instance can be run on the system. If it is already run under a different security context your code will not be able to get a running instance by using the
Newoperator.