J'utilise la macro Excel VBA pour envoyer des courriels automatisés (Outlook 2013) qui s'exécute avec le planificateur de tâches Windows (j'utilise le fichier de commandes) à l'heure spécifiée chaque jour. Lorsque j'exécute ma macro sans Task Scheduler, il s'exécute normalement (les emails sont envoyés), mais quand j'utilise Task Scheduler pour cela je reçois "run-time error 429", cela arrive seulement quand la macro VBA essaye de créer un objet Outlook:Le code VBA Excel génère une erreur d'exécution 429 lors de l'envoi de courrier électronique Outlook à l'aide du planificateur de tâches
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application") 'The error happens here
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.to = "[email protected]"
.CC = ""
.BCC = ""
.Subject = "subj"
.Body = "body"
.Attachments.Add ActiveWorkbook.FullName
.Send
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
L'erreur ci-dessus se produit uniquement si l'application Outlook est ouverte sur l'ordinateur. Maintenant, ce que je ne comprends pas:
Pourquoi le travail macro normalement sans le Planificateur de tâches (en dépit d'Outlook être ouvert ou non) et pourquoi ne pas travailler là-bas? Comment faire en sorte que l'ensemble du processus s'exécute en utilisant le Planificateur de tâches et ne pas dépendre de l'ouverture ou de la fermeture de l'application Outlook? (c'est-à-dire que je veux que la macro s'exécute quelles que soient les applications ouvertes/fermées).
Un conseil serait grandement apprécié.
Edit: Voici le code VBScript J'utilise pour exécuter macro (en repsonse à la question de LS_ᴅᴇᴠ):
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
' Create an Excel instance
Dim myExcelWorker
Set myExcelWorker = CreateObject("Excel.Application")
' Disable Excel UI elements
myExcelWorker.DisplayAlerts = False
myExcelWorker.AskToUpdateLinks = False
myExcelWorker.AlertBeforeOverwriting = False
myExcelWorker.FeatureInstall = msoFeatureInstallNone
' Tell Excel what the current working directory is
' (otherwise it can't find the files)
Dim strSaveDefaultPath
Dim strPath
strSaveDefaultPath = myExcelWorker.DefaultFilePath
strPath = WshShell.CurrentDirectory
myExcelWorker.DefaultFilePath = strPath
' Open the Workbook specified on the command-line
Dim oWorkBook
Dim strWorkerWB
strWorkerWB = strPath & "\____DailyReport.xlsm"
Set oWorkBook = myExcelWorker.Workbooks.Open(strWorkerWB)
' Build the macro name with the full path to the workbook
Dim strMacroName
strMacroName = "'" & strPath & "\____DailyReport.xlsm'" & "!Module1.____DailyRep"
on error resume next
' Run the calculation macro
myExcelWorker.Run strMacroName
if err.number <> 0 Then
' Error occurred - just close it down.
End If
err.clear
on error goto 0
'oWorkBook.Save
'oWorkBook.Close <<--- we don't need these two because we close the WB in the VBA macro
myExcelWorker.DefaultFilePath = strSaveDefaultPath
' Clean up and shut down
Set oWorkBook = Nothing
' Don’t Quit() Excel if there are other Excel instances
' running, Quit() will
'shut those down also
if myExcelWorker.Workbooks.Count = 0 Then
myExcelWorker.Quit
End If
Set myExcelWorker = Nothing
Set WshShell = Nothing
Étant une macro Excel, quel lot avez-vous mis dans le planificateur de tâches? –
@ LS_ᴅᴇᴠ J'utilisais le script VBScript décrit ici: [Excel: Exécuter Excel sur le planificateur de tâches Windows] (http://krgreenlee.blogspot.com/2006/04/excel-running-excel-on-windows-task.html) et mon fichier batch ressemblait à ceci: 'start" "" D: \ Documents \ Macros \ ___ \ Run__Rep.vbs "' –
Donc, une autre question ... Que contient VBS? –