Bonjour,

Je galère depuis quelque temps avec un script me permettant d'envoyer à mes client un mail personnalisé avec une pièce jointe. Tout fonctionne à peu près bien mais de temps à autre les fichiers change de point de sauvegarde et je n'arrive pas à définir un point de sauvegarde pour les fichiers word convertis en pdf.

Je vous remercie par avance !

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
----------------------
--Main Programme
----------------------
 
set startCell to "A1" as string
set leSujet to "CES EduPack 2013 – Maintenance de votre installation"
--set leCorps to ("Bonjour " & leSujet)
 
display dialog "Enter the Name of the last cell in Excel " default answer "AA25"
set endCell to text returned of the result
 
set myMatrix to excelTxtExtract(startCell, endCell)
set custInfo to findInfo(myMatrix, 1) --récupération des mots clés
 
set partChoix to choose from list custInfo with prompt "Choose part(s) for file name :" with multiple selections allowed
set numList to posElmList(partChoix, custInfo)
--display dialog posList as string
 
 
set myFilenameWord to (choose file with prompt "Please select the Word Template doc for the Quotation :")
 
set partChoix to choose from list custInfo with prompt "Choose the email address :"
set emailList to posElmList(partChoix, custInfo)
--display dialog emailList as string
 
set partChoix2 to choose from list custInfo with prompt "Choose Title of the person :"
set title1 to posElmList(partChoix2, custInfo)
 
set partChoix3 to choose from list custInfo with prompt "Choose the Name of hte person :"
set name1 to posElmList(partChoix3, custInfo)
 
set partChoix4 to choose from list custInfo with prompt "Choose the Nbr of User of the Lycée :"
set seat1 to posElmList(partChoix4, custInfo)
 
set partChoix5 to choose from list custInfo with prompt "Choose the licence type:"
set licenceType1 to posElmList(partChoix5, custInfo)
 
set partChoix6 to choose from list custInfo with prompt "Choose the maintenance expire date:"
set expireMaint1 to posElmList(partChoix6, custInfo)
 
repeat with i from 2 to (length of myMatrix)
	set customer to findInfo(myMatrix, i)
	set email to nomFichier(emailList, customer) as string
 
	set title2 to nomFichier(title1, customer) as string
	set name2 to nomFichier(name1, customer) as string
	set seat2 to nomFichier(seat1, customer) as string
	set licenceType2 to nomFichier(licenceType1, customer) as string
	set expireMaint2 to nomFichier(expireMaint1, customer) as string
 
	set leCorps to "Bonjour " & title2 & " " & name2 & "," & return & return & "TEXTE"
 
	set nomDuFichier to nomFichier(numList, customer) as string
	wordTxtModif(myFilenameWord, customer, custInfo, nomDuFichier)
	envoiMail(leSujet, leCorps, email, nomDuFichier)
end repeat
 
----------------------
--Envoyer Mail
----------------------
 
on envoiMail(leSujet, leCorps, email, nomDuFichier)
	tell application "Mail"
		activate
		set nouveauMessage to make new outgoing message with properties {subject:leSujet, content:leCorps & return & return, visible:false}
		tell nouveauMessage
			make new to recipient at end of to recipients with properties {address:email}
			tell content of nouveauMessage
				make new attachment with properties {file name:("/Users/grantadesign/Documents/" & nomDuFichier & ".pdf")} at after the last paragraph
			end tell
			send nouveauMessage
		end tell
	end tell
end envoiMail
 
----------------------
--Nom du fichier 
--Retourne le nom du fichier sans l'extension "Lycée F.Xavier"
----------------------
on nomFichier(numList, customer)
	activate
	set nomDuFichier to {}
	repeat with i from 1 to (length of numList)
		set a to item i of numList
		copy (item a of customer) to the end of nomDuFichier
	end repeat
	return nomDuFichier
end nomFichier
 
----------------------
--Position d'un élément dans une lste
----------------------
on posElmList(partChoix, custInfo)
	activate
	set elmChoix to 0
	set numList to {}
	repeat with i from 1 to (length of custInfo)
		set elmChoix to elmChoix + 1
		repeat with j from 1 to (length of partChoix)
			if ((item j of partChoix) = (item i of custInfo)) then
				copy elmChoix to the end of numList
			end if
		end repeat
	end repeat
	return numList
end posElmList
 
 
 
----------------------
--Modifier un document Word
--remplacer des mots dans un texte
----------------------
 
on wordTxtModif(myFilenameWord, customer, custInfo, nomDuFichier)
	tell application "Microsoft Word" -- 
		activate
		open myFilenameWord
		set findrange to find object of selection -- ???
		repeat with i from 1 to (length of custInfo)
			tell findrange --replacement of the bookmark
				execute find find text (item i of custInfo) replace with (item i of customer) replace replace all
			end tell
 
		end repeat
		--display dialog nomDuFichier as string
		save as active document file name (nomDuFichier & ".pdf") file format format PDF
		close window 1 saving no
	end tell
end wordTxtModif
 
----------------------
--Chargement des valeurs Excel dans une liste
--Ammélioration : trouver startCell et endCell automatiquement
----------------------
on excelTxtExtract(startCell, endCell)
	tell application "Microsoft Excel" -- Load values from Excel
		activate
		set myFilenameExcel to choose file with prompt "Please select an Excel workbook file:"
		open myFilenameExcel
		set nameworksheet to get name of every worksheet
		set myWorksheet to choose from list nameworksheet default items (item 1 of nameworksheet) with prompt "Choose a worksheet where the data are in the list:"
		activate object worksheet (result as string)
		set myMatrix to value of range (startCell & ":" & endCell)
		return (myMatrix)
	end tell
end excelTxtExtract
 
----------------------
--Trouver automatiquement les Mots clés à remplacer dans le texte
--lineChosen est la ligne que l'on veut lire
----------------------
on findInfo(myMatrix, lineChosen)
	activate
	set custInfo to {}
	set selectLine to (item lineChosen of myMatrix)
	repeat with i from 1 to (length of selectLine)
		set custInfo to custInfo & (item i of selectLine)
	end repeat
	return custInfo
end findInfo