interaction with excel is working but not working with outlook
I am able to schedule the below task in the task scheduler in " user is logged on or not" mode and it is working fine. (running from VBS file)
Sub runTaskTest()
Dim erow As Long
erow = Sheets(1).Cells(Rows.Count, "A").End(xlUp).Row
Sheets(1).Cells(erow + 1, 1).Value = "This test was successful : " & Now
ThisWorkbook.Saved = True
ThisWorkbook.Save
End Sub
but when i try to send an automatic email from outlook using excel vba macro scheduler is failed to run with the option "whether user is logged on or not"
Sub runTaskTest()
Dim OutlookApp As Outlook.Application
Dim OutlookMail As Outlook.MailItem
Set OutlookApp = New Outlook.Application
Set OutlookMail = OutlookApp.CreateItem(olMailItem)
With OutlookMail
.BodyFormat = olFormatHTML
.Display
.HTMLBody = "Dear ABC" & "<br>" & "<br>" & "Please find the attached file" &
.HTMLBody
'last .HTMLBody includes signature from the outlook.
''<br> includes line breaks
b/w two lines
.To = "[email protected]"
.Subject = "Test mail"
.Attachments = ThisWorkbook
.Send
End With
End Sub
VBScript
Option Explicit
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("D:\Excel_Test\excel_test.xlsm", 0, False)
xlApp.DisplayAlerts = False
xlApp.Visible = False
xlApp.Run "'excel_test.xlsm'!runTaskTest" // **
xlBook.Saved = True
xlBook.Save
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
but same code is working fine when I select " Run only when user logged on"
I tried the below solutions but nothing helps
created empty folder in "C:\Windows\System32\config\systemprofile\Desktop" refered enter link description here
checked all the privileges and rights to run the tasks( log on as batch job )
but nothing helps. kindly help me to resolve the issue.
Consider using EWS instead if you deal with Exchange accounts only, see Explore the EWS Managed API, EWS, and web services in Exchange for more information.
The Considerations for server-side Automation of Office article says the following:
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.
You may also consider using a low-level API on which Outlook is based on (Extended MAPI) from a windows service or tasks run by the Windows scheduler. For example, you may consider using any third-party components built on top that API such as Redemption.