Je reviens par ici car je rencontre un problème sur un script que je suis en train d'écrire, dont voilà grossièrement le fonctionnement : on exécute le script qui, après quelques vérifications, lance OOO. A la fermeture, le script envoie à une adresse donnée le tableau ainsi rempli, ainsi qu'un second tableau qui est crée via un macro dans OOO.
Mon problème ? tout marche bien, sauf que je n'arrive pas à avoir les pièces jointes ! le mail se nomme bien, est bien envoyé, mais vide ...
;******************************************************************
;* SCRIPT D'AUTOMATISATION D'ENVOI DE FICHIER *
;* Ecrit par Ghost Line*
;* Juin 2009 *
;******************************************************************
;Bréviaire des erreurs :
;ERREUR FICHIER n°1 = le fichier original n'est pas présent à l'emplacement prévu
;ERREUR FICHIER n°2 = le fichier copié n'est pas modifiable en "non lecture seule"
;ERREUR FICHIER n°3 = le fichier n'a pas été copié dans le repertoire destination
;ERREUR FICHIER n°4 = le repertoire de transit ne contient pas de fichier ODS après la sauvegarde des modifications
;ERREUR FICHIER n°5 = il n'y a qu'un seul fichier dans le repertoire de transit (non bloquant)
#include <INet.au3>
#Include <File.au3>
; Phase déclarative
$NomFichierOriginal = "planning_original.ods"
$NomFichierCopie = "planning_copie.ods"
$CheminFichierOriginal = (@HomeDrive&"\windows\system32\"&$NomFichierOriginal)
$RepertoireBackup = (@MyDocumentsDir&"\planning\"&@YEAR&"\"&@MON&"\")
$RepertoireTransit = (@HomeDrive&"\windows\planning\")
$Contact = " : contactez le Service Informatique."
; On vérifie que les repertoires déclarés existent
If Not FileExists($RepertoireBackup&".") then
DirCreate($RepertoireBackup)
EndIf
; On vérifie que le fichier existe bien à l'emplacement convenu
$verif = FileExists($CheminFichierOriginal)
If $verif Then
Else
MsgBox(0,"ERREUR !","ERREUR FICHIER n°1"&$Contact)
Exit
EndIf
; On relève la taille du fichier original, puis on en crée une copie (et on crée le repertoire au besoin)
$taille_original=FileGetSize($CheminFichierOriginal)
FileCopy($CheminFichierOriginal,$RepertoireTransit&$NomFichierCopie,8)
; On retire l'attribut Lecture Seule hérité de l'original à la copie (et on vérifie que la copie s'est bien passée)
If FileExists($RepertoireTransit&$NomFichierCopie) Then
If Not FileSetAttrib($RepertoireTransit&$NomFichierCopie, "-R") Then
MsgBox(0,"ERREUR !", "ERREUR FICHIER n°2"&$Contact)
Exit
EndIf
Else
MsgBox(0,"ERREUR !", "ERREUR FICHIER n°3"&$Contact)
Exit
EndIf
; On lance Open Office sur la copie et on attend la fermeture de Open Office pour continuer le script
ShellExecuteWait(@ProgramFilesDir&"\OpenOffice.org 3\program\scalc.exe",$RepertoireTransit&$NomFichierCopie)
; On compare la taille du fichier original et du fichier édité
; Si ils font la même taille, on arrête la boucle.
; On récupère le nom des deux premiers fichiers du repertoire
$Recherche = FileFindFirstFile($RepertoireTransit&"*.*")
$PremierFichier = FileFindNextFile($Recherche)
If $PremierFichier = -1 Then
MsgBox(0, "ERREUR !", "ERREUR FICHIER n°4"&$Contact)
Exit
EndIf
$SecondFichier = FileFindNextFile($Recherche)
If $SecondFichier = "" Then
MsgBox(0, "ERREUR !", "ERREUR FICHIER n°5"&$Contact)
; Erreur non bloquante : il manque le deuxième fichier
EndIf
; On demande confirmation pour l'expédition du mail
$Confirmation = MsgBox(4, "Confirmation", "Confirmez-vous souhaiter envoyer ce planning ?")
if $Confirmation = 6 Then
; Si clic sur "Yes", on expédie les deux premiers fichiers du repertoire de transit à une adresse mail donnée
$as_Body = ""
$s_SmtpServer = "smtp.fr.totoro.com"
$s_FromName = "Planning magasin"
$s_FromAddress = "b@b.com"
$s_ToAddress = "a@a.fr"
$s_AttachFiles =$RepertoireTransit&$PremierFichier&";"&$RepertoireTransit&$SecondFichier
$s_Subject = $PremierFichier
$Response = _INetSmtpMail ($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body)
Else
; Sinon, on propose de relancer Open Office pour modification, et si la réponse est "No", on quitte
$Confirmation2 = MsgBox(4,"Confirmation","Souhaitez-vous éditer de nouveau le planning ?")
If $Confirmation2 = 6 Then
;Boucle à rattraper : on retourne en ligne 53
Else
Exit
EndIf
EndIf
; Une fois le mail envoyé, on déplace les fichiers dans un répertoire de backup
FileMove($RepertoireTransit&"*.*",$RepertoireBackup)
; Fin du programme !
Modifié en dernier par GhostLine le mer. 01 juil. 2009 16:09, modifié 1 fois.
;******************************************************************
;* SCRIPT D'AUTOMATISATION D'ENVOI DE FICHIER *
;* Ecrit par Ghost Line*
;* Juin 2009 *
;******************************************************************
;Bréviaire des erreurs :
;ERREUR FICHIER n°1 = le fichier original n'est pas présent à l'emplacement prévu
;ERREUR FICHIER n°2 = le fichier copié n'est pas modifiable en "non lecture seule"
;ERREUR FICHIER n°3 = le fichier n'a pas été copié dans le repertoire destination
;ERREUR FICHIER n°4 = le repertoire de transit ne contient pas de fichier ODS après la sauvegarde des modifications
;ERREUR FICHIER n°5 = il n'y a qu'un seul fichier dans le repertoire de transit (non bloquant)
;~ #include <INet.au3>
#Include <File.au3>
#include <mail.au3>
; Phase déclarative
$NomFichierOriginal = "planning_original.ods"
$NomFichierCopie = "planning_copie.ods"
$CheminFichierOriginal = (@HomeDrive&"\windows\system32\"&$NomFichierOriginal)
$RepertoireBackup = (@MyDocumentsDir&"\planning\"&@YEAR&"\"&@MON&"\")
$RepertoireTransit = (@HomeDrive&"\windows\planning\")
$Contact = " : contactez le Service Informatique."
; Déclaration paramètres mail.au3
$s_SmtpServer = "smtp.xxxx.com" ; address for the smtp-server to use - REQUIRED
$s_FromName = "Reporting planning" ; name from who the email was sent
$s_FromAddress = "informatique@xxxx.com" ; address from where the mail should come
$s_ToAddress = "tonari_no_totoro@xxxx.fr" ; destination address of the email - REQUIRED
$s_CcAddress = "" ; address for cc - leave blank if not needed
$s_BccAddress = "" ; address for bcc - leave blank if not needed
$s_Username = "xxxx@xxxx" ; username for the account used from where the mail gets sent - Optional (Needed for eg GMail)
$s_Password = "xxxx" ; password for the account used from where the mail gets sent - Optional (Needed for eg GMail)
$IPPort = 25 ; port used for sending the mail
$ssl = 0 ; enables/disables secure socket layer sending - put to 1 if using httpS
;~ $IPPort=465 ; GMAIL port used for sending the mail
;~ $ssl=1 ; GMAILenables/disables secure socket layer sending - put to 1 if using httpS
; On vérifie que les repertoires déclarés existent
If Not FileExists($RepertoireBackup&".") then
DirCreate($RepertoireBackup)
EndIf
; On vérifie que le fichier existe bien à l'emplacement convenu
$verif = FileExists($CheminFichierOriginal)
If $verif Then
Else
MsgBox(0,"ERREUR !","ERREUR FICHIER n°1"&$Contact)
Exit
EndIf
; On relève la taille du fichier original, puis on en crée une copie (et on crée le repertoire au besoin)
$taille_original=FileGetSize($CheminFichierOriginal)
FileCopy($CheminFichierOriginal,$RepertoireTransit&$NomFichierCopie,8)
; On retire l'attribut Lecture Seule hérité de l'original à la copie (et on vérifie que la copie s'est bien passée)
If FileExists($RepertoireTransit&$NomFichierCopie) Then
If Not FileSetAttrib($RepertoireTransit&$NomFichierCopie, "-R") Then
MsgBox(0,"ERREUR !", "ERREUR FICHIER n°2"&$Contact)
Exit
EndIf
Else
MsgBox(0,"ERREUR !", "ERREUR FICHIER n°3"&$Contact)
Exit
EndIf
; On lance Open Office sur la copie et on attend la fermeture de Open Office pour continuer le script
ShellExecuteWait(@ProgramFilesDir&"\OpenOffice.org 3\program\scalc.exe",$RepertoireTransit&$NomFichierCopie)
; On compare la taille du fichier original et du fichier édité
; Si ils font la même taille, on arrête la boucle.
; On récupère le nom des deux premiers fichiers du repertoire
$Recherche = FileFindFirstFile($RepertoireTransit&"*.*")
$PremierFichier = FileFindNextFile($Recherche)
If $PremierFichier = -1 Then
MsgBox(0, "ERREUR !", "ERREUR FICHIER n°4"&$Contact)
Exit
EndIf
$SecondFichier = FileFindNextFile($Recherche)
If $SecondFichier = "" Then
MsgBox(0, "ERREUR !", "ERREUR FICHIER n°5"&$Contact)
; Erreur non bloquante : il manque le deuxième fichier
EndIf
; On demande confirmation pour l'expédition du mail
$Confirmation = MsgBox(4, "Confirmation", "Confirmez-vous souhaiter envoyer ce planning ?")
if $Confirmation = 6 Then
; Si clic sur "Oui", on expédie les deux premiers fichiers du repertoire de transit à une adresse mail donnée
$as_Body = ""
$s_AttachFiles =$RepertoireTransit&$PremierFichier&";"&$RepertoireTransit&$SecondFichier
$s_Subject = $PremierFichier
_INetSmtpMailCom ($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body,$s_AttachFiles)
Else
; Sinon, on propose de relancer Open Office pour modification, et si la réponse est "No", on quitte
$Confirmation2 = MsgBox(4,"Confirmation","Souhaitez-vous éditer de nouveau le planning ?")
If $Confirmation2 = 6 Then
;Boucle à rattraper : on retourne en ligne 53
Else
Exit
EndIf
EndIf
; Une fois le mail envoyé, on déplace les fichiers dans un répertoire de backup
FileMove($RepertoireTransit&"*.*",$RepertoireBackup)
; Fin du programme !
;##################################
; Include
;##################################
#Include<file.au3>
;##################################
; Variables
;##################################
$s_Subject = "" ; subject from the email - can be anything you want it to be
$as_Body = "" ; the messagebody from the mail - can be left blank but then you get a blank mail
$s_AttachFiles = "" ; the file you want to attach- leave blank if not needed
$s_SmtpServer = "smtp.xxxx.com" ; address for the smtp-server to use - REQUIRED
$s_FromName = "" ; name from who the email was sent
$s_FromAddress = "informatique@xxxx.com" ; address from where the mail should come
$s_ToAddress = "tonari_no_totoro@xxxx.fr" ; destination address of the email - REQUIRED
$s_CcAddress = "" ; address for cc - leave blank if not needed
$s_BccAddress = "" ; address for bcc - leave blank if not needed
$s_Username = "xxxx@xxxx"; username for the account used from where the mail gets sent - Optional (Needed for eg GMail)
$s_Password = "xxxx" ; password for the account used from where the mail gets sent - Optional (Needed for eg GMail)
$IPPort = 25 ; port used for sending the mail
$ssl = 0 ; enables/disables secure socket layer sending - put to 1 if using httpS
;~ $IPPort=465 ; GMAIL port used for sending the mail
;~ $ssl=1 ; GMAILenables/disables secure socket layer sending - put to 1 if using httpS
;##################################
; Script
;##################################
Global $oMyRet[2]
Global $oMyError = ObjEvent("AutoIt.Error", "MyErrFunc")
;$rc = _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, $s_AttachFiles, $s_CcAddress, $s_BccAddress, $s_Username, $s_Password, $IPPort, $ssl)
If @error Then
MsgBox(0, "Error sending message", "Error code:" & @error & " Rc:" & $rc)
EndIf
;
Func _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject = "", $as_Body = "", $s_AttachFiles = "", $s_CcAddress = "", $s_BccAddress = "", $s_Username = "", $s_Password = "",$IPPort=25, $ssl=0)
$objEmail = ObjCreate("CDO.Message")
$objEmail.From = '"' & $s_FromName & '" <' & $s_FromAddress & '>'
$objEmail.To = $s_ToAddress
Local $i_Error = 0
Local $i_Error_desciption = ""
If $s_CcAddress <> "" Then $objEmail.Cc = $s_CcAddress
If $s_BccAddress <> "" Then $objEmail.Bcc = $s_BccAddress
$objEmail.Subject = $s_Subject
If StringInStr($as_Body,"<") and StringInStr($as_Body,">") Then
$objEmail.HTMLBody = $as_Body
Else
$objEmail.Textbody = $as_Body & @CRLF
EndIf
If $s_AttachFiles <> "" Then
Local $S_Files2Attach = StringSplit($s_AttachFiles, ";")
For $x = 1 To $S_Files2Attach[0]
$S_Files2Attach[$x] = _PathFull ($S_Files2Attach[$x])
If FileExists($S_Files2Attach[$x]) Then
$objEmail.AddAttachment ($S_Files2Attach[$x])
Else
$i_Error_desciption = $i_Error_desciption & @lf & 'File not found to attach: ' & $S_Files2Attach[$x]
SetError(1)
return 0
EndIf
Next
EndIf
$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = $s_SmtpServer
$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = $IPPort
;Authenticated SMTP
If $s_Username <> "" Then
$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = $s_Username
$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = $s_Password
EndIf
If $Ssl Then
$objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
EndIf
;Update settings
$objEmail.Configuration.Fields.Update
; Sent the Message
$objEmail.Send
if @error then
SetError(2)
return $oMyRet[1]
EndIf
EndFunc ;==>_INetSmtpMailCom
;
;
; Com Error Handler
Func MyErrFunc()
$HexNumber = Hex($oMyError.number, 8)
$oMyRet[0] = $HexNumber
$oMyRet[1] = StringStripWS($oMyError.description,3)
ConsoleWrite("### COM Error ! Number: " & $HexNumber & " ScriptLine: " & $oMyError.scriptline & " Description:" & $oMyRet[1] & @LF)
SetError(1); something to check for when this function returns
Return
EndFunc ;==>MyErrFunc
Nota : la mise en commentaire de la ligne ;$rc = _INetSmtpMailCom($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body, $s_AttachFiles, $s_CcAddress, $s_BccAddress, $s_Username, $s_Password, $IPPort, $ssl)
est la méthode la plus rapide (et gorette) que j'aie pu trouver pour éviter de recevoir deux mails au lieu d'un seul.
Modifié en dernier par GhostLine le ven. 03 juil. 2009 10:45, modifié 1 fois.
;*******************************************************************
;* SCRIPT D'AUTOMATISATION D'ENVOI DE FICHIER *
;* Ecrit par GHOST LINE *
;* Juin 2009 *
;*******************************************************************
;Bréviaire des erreurs :
;ERREUR FICHIER n°1 = le fichier original n'est pas présent à l'emplacement prévu
;ERREUR FICHIER n°2 = le fichier copié n'est pas modifiable en "non lecture seule"
;ERREUR FICHIER n°3 = le fichier n'a pas été copié dans le repertoire destination
;ERREUR FICHIER n°4 = le repertoire de transit ne contient pas de fichier ODS après la sauvegarde des modifications
;ERREUR FICHIER n°5 = il n'y a qu'un seul fichier dans le repertoire de transit (non bloquant)
;ERREUR FICHIER n°6 = le fichier original et la copie après modification font la même taille
#Include <File.au3>
#include <mail.au3>
; Phase déclarative
$NomFichierOriginal = "planning_original.ods"
$NomFichierCopie = "planning_copie.ods"
$CheminFichierOriginal = (@HomeDrive&"\windows\system32\"&$NomFichierOriginal)
$RepertoireBackup = (@MyDocumentsDir&"\planning\"&@YEAR&"\"&@MON&"\")
$RepertoireTransit = (@HomeDrive&"\windows\planning\")
$Contact = " : contactez le Service Informatique."
; Déclaration paramètres mail.au3
$s_SmtpServer = "smtp.totoro.com" ; address for the smtp-server to use - REQUIRED
$s_FromName = "Reporting planning" ; name from who the email was sent
$s_FromAddress = "informatique@micro$oft.com" ; address from where the mail should come
$s_ToAddress = "totoro@micro$oft.fr" ; destination address of the email - REQUIRED
$s_CcAddress = "" ; address for cc - leave blank if not needed
$s_BccAddress = "" ; address for bcc - leave blank if not needed
$s_Username = "***" ; username for the account used from where the mail gets sent - Optional (Needed for eg GMail)
$s_Password = "***" ; password for the account used from where the mail gets sent - Optional (Needed for eg GMail)
$IPPort = 25 ; port used for sending the mail
$ssl = 0 ; enables/disables secure socket layer sending - put to 1 if using httpS
;~ $IPPort=465 ; GMAIL port used for sending the mail
;~ $ssl=1 ; GMAILenables/disables secure socket layer sending - put to 1 if using httpS
; On vérifie que les repertoires déclarés existent
If Not FileExists($RepertoireBackup&".") then
DirCreate($RepertoireBackup)
EndIf
; On vérifie que le fichier existe bien à l'emplacement convenu
$verif = FileExists($CheminFichierOriginal)
If $verif Then
Else
MsgBox(0,"ERREUR !","ERREUR FICHIER n°1"&$Contact)
Exit
EndIf
; On relève la taille du fichier original, puis on en crée une copie (et on crée le repertoire au besoin)
$taille_original=FileGetSize($CheminFichierOriginal)
FileCopy($CheminFichierOriginal,$RepertoireTransit&$NomFichierCopie,9)
; On retire l'attribut Lecture Seule hérité de l'original à la copie (et on vérifie que la copie s'est bien passée)
If FileExists($RepertoireTransit&$NomFichierCopie) Then
If Not FileSetAttrib($RepertoireTransit&$NomFichierCopie, "-R") Then
MsgBox(0,"ERREUR !", "ERREUR FICHIER n°2"&$Contact)
Exit
EndIf
Else
MsgBox(0,"ERREUR !", "ERREUR FICHIER n°3"&$Contact)
Exit
EndIf
; On lance Open Office sur la copie et on attend la fermeture de Open Office pour continuer le script
ShellExecuteWait(@ProgramFilesDir&"\OpenOffice.org 3\program\scalc.exe",$RepertoireTransit&$NomFichierCopie)
; On compare la taille du fichier original et du fichier édité
$taille_copie=FileGetSize($RepertoireTransit&FileFindNextFile(FileFindFirstFile($RepertoireTransit&"*.ods")))
If $taille_copie = $taille_original Then
MsgBox(0, "ERREUR !", "ERREUR FICHIER n°6"&$Contact)
Exit
EndIf
; On récupère le nom des deux premiers fichiers du repertoire
$Recherche = FileFindFirstFile($RepertoireTransit&"*.*")
$PremierFichier = FileFindNextFile($Recherche)
If $PremierFichier = -1 Then
MsgBox(0, "ERREUR !", "ERREUR FICHIER n°4"&$Contact)
Exit
EndIf
; Test de présence du second fichier (erreur non bloquante)
$SecondFichier = FileFindNextFile($Recherche)
If $SecondFichier = "" Then
MsgBox(0, "ERREUR NON BLOQUANTE !", "ERREUR FICHIER n°5"&$Contact)
EndIf
; On demande confirmation pour l'expédition du mail
$Confirmation = MsgBox(4, "Confirmation", "Confirmez-vous souhaiter envoyer ce planning ?")
if $Confirmation = 6 Then
; Si clic sur "Oui", on expédie les deux premiers fichiers du repertoire de transit à une adresse mail donnée
$as_Body = ""
$s_AttachFiles =$RepertoireTransit&$PremierFichier&";"&$RepertoireTransit&$SecondFichier
$s_Subject = $PremierFichier
_INetSmtpMailCom ($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body,$s_AttachFiles)
Else
Exit
EndIf
; Une fois le mail envoyé, on déplace les fichiers dans un répertoire de backup
FileMove($RepertoireTransit&"*.*",$RepertoireBackup)
Modifié en dernier par GhostLine le ven. 03 juil. 2009 10:44, modifié 1 fois.