Bonjour,
après avoir ouvert un premier post sur le sujet je rencontre parfois un problème lors de l'execution de mon BGW.
la valeur de ma variable globale sPath (qui est le chemin utiliser pour attacher un doc lorsque j'envoie un mail) et pas VU dans ma fonction d'envoi de mail)
je souhaite que le BGW traite la creation d'un .doc et également la conversion en PDF
pour cela j'ai codé ceci :
1) envoi d'email sur l'event click (dans cette fonction est appelé mon BGW):
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 private void SendMailClient(string Caller) { try { FileInfo fi; string sFileToAttach = string.Empty; openFileDialog1.Multiselect = false; openFileDialog1.CheckFileExists = true; openFileDialog1.CheckPathExists = true; String sSource = string.Empty; Microsoft.Office.Interop.Outlook.Application oApp = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.MailItem oMsg = (Microsoft.Office.Interop.Outlook.MailItem)oApp.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); Microsoft.Office.Interop.Outlook.Recipient oRecip;//= (Microsoft.Office.Interop.Outlook.Recipient)oMsg.Recipients.Add(tBoxMailClient.Text); Microsoft.Office.Interop.Outlook.Attachment oAttach; openFileDialog1.Filter = "All files (*.*)|*.*|Documents files (*.doc)|*.doc|Images files (*.jpg)|*.jpg|PDF files (*.pdf)|*.pdf"; if ((tBoxMailClient.Text.Length > 1) && (tBoxMailClient.Text.Contains("@")) && (tBoxMailClient.Text.Contains(".")) || (Caller == "Mailing")) { if (Caller != "Mailing") { KillPDF(); BGW_PDF.RunWorkerAsync(); } else if ((Caller == "Mailing") && (CBAttachementC.Checked == true)) { if (openFileDialog1.ShowDialog() == DialogResult.OK) { fi = new System.IO.FileInfo(openFileDialog1.FileName); if (fi.Name.Length > 0) { sFileToAttach = fi.FullName; } Environment.CurrentDirectory = sPathOrg; } } if (Caller != "Mailing") { oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oMsg.Recipients.Add(tBoxMailClient.Text); oRecip.Resolve(); } else { oMsg.BCC = sMailing.Substring(1); } //Set the basic properties. oMsg.Subject = "[EURO MEDIA]"; oMsg.Body = tboxMailMsg.Text; if (Caller != "Mailing") { sSource = sPathPDF; String sDisplayName = "MyFirstAttachment"; int iPosition = (int)oMsg.Body.Length + 1; int iAttachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue; oAttach = oMsg.Attachments.Add(sSource, iAttachType, iPosition, sDisplayName); } else if ((Caller == "Mailing") && (CBAttachementC.Checked == true) && (sFileToAttach != string.Empty)) { sSource = sFileToAttach; String sDisplayName = "MyFirstAttachment"; int iPosition = (int)oMsg.Body.Length + 1; int iAttachType = (int)Microsoft.Office.Interop.Outlook.OlAttachmentType.olByValue; oAttach = oMsg.Attachments.Add(sSource, iAttachType, iPosition, sDisplayName); } // oMsg.Display(true); //modal oMsg.Save(); (oMsg as Microsoft.Office.Interop.Outlook._MailItem).Send(); oRecip = null; oAttach = null; oMsg = null; oApp = null; if (Caller != "Mailing") File.Delete(sPath); } else { MessageBox.Show("Veuillez saisir un format d'email correct", "Erreur de Format", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception e1) { MessageBox.Show("Exception caught: " + e1); } }
Voici ce que contient la methode DoWork de mon BGW:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 private void BGW_PDF_DoWork(object sender, DoWorkEventArgs e) { CreateDoc(iTypeDoc, false); ExportToPDF(); }
2) creation du .doc a partir des infos de ma form:
3 : Convertion du doc en PDF
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 public void CreateDoc(int NumRelance, bool IsVisible) { string sPathSaveFacture = string.Empty; Microsoft.Office.Interop.Word.Application msWord = new Microsoft.Office.Interop.Word.Application(); msWord.Visible = false; object missing = System.Reflection.Missing.Value; object fileName = string.Empty; sPathSaveFacture = Environment.CurrentDirectory + "\\Content\\" + DateTime.Now.Year.ToString(); fileName = @sPathSaveFacture + "\\" +"Facture_" + tBoxFacture.Text + "_" + tBoxRaisonSocialeClient.Text + "_" + sDateFile + ".doc"; sNomDocSansExtension = "Facture_" + tBoxFacture.Text + "_" + tBoxRaisonSocialeClient.Text + "_" + sDateFile; sPathPDF = @sPathSaveFacture + "\\" + "Facture_" + tBoxFacture.Text + "_" + tBoxRaisonSocialeClient.Text + "_" + sDateFile + ".pdf"; Microsoft.Office.Interop.Word.Document nvDoc; if (File.Exists((string)fileName)) { // ouvrir le document existant nvDoc = msWord.Documents.Open(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); try { // nvDoc.Unprotect(ref pwd); } catch {} } else { // Choisir le template object templateName = ""; templateName = Environment.CurrentDirectory + "\\Content\\"+sModelCourierTypeFacture;break; //FactureEMV3.dot"; break; // Créer le document nvDoc = msWord.Documents.Add(ref templateName, ref missing, ref missing, ref missing); } object field = "NumFacture"; nvDoc.FormFields.get_Item(ref field).Result = tBoxFacture.Text; field = "Raison_Social"; nvDoc.FormFields.get_Item(ref field).Result = tBoxRaisonSocialeClient.Text; field = "Adresse"; nvDoc.FormFields.get_Item(ref field).Result = tBoxAdresseClient.Text; field = "CP"; nvDoc.FormFields.get_Item(ref field).Result = tBoxCPClient.Text; field = "Ville"; nvDoc.FormFields.get_Item(ref field).Result = tBoxVille.Text; object noReset = true; object useIRM = true; object enforce = true; // Sauver le document nvDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing); //sPath est utilisé pour l'attachement au mail sPath = fileName.ToString(); // nvDoc.PrintPreview(); (nvDoc as Microsoft.Office.Interop.Word._Document).Close(ref missing, ref missing, ref missing); (msWord as Microsoft.Office.Interop.Word._Application).Quit(ref missing, ref missing, ref missing); }
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 private void ExportToPDF() { PrintDocument prtdoc = new PrintDocument(); DefPrinter = prtdoc.PrinterSettings.PrinterName; //CreateDoc(iTypeDoc, false); string fname, DefaultPrinter; string fi2 = string.Empty; PDFCreator.clsPDFCreatorOptions opt; pErr = new PDFCreator.clsPDFCreatorError(); _PDFCreator = new PDFCreator.clsPDFCreator(); _PDFCreator.eError += new PDFCreator.__clsPDFCreator_eErrorEventHandler(_PDFCreator_eError); _PDFCreator.eReady += new PDFCreator.__clsPDFCreator_eReadyEventHandler(_PDFCreator_eReady); string parameters = "/NoProcessingAtStartup"; if (!_PDFCreator.cStart(parameters, false)) { MessageBox.Show("Status: Error[" + pErr.Number + "]: " + pErr.Description); } else fi2 = sPath; fname = sNomDocSansExtension; if (!_PDFCreator.cIsPrintable(sPath)) { MessageBox.Show("File '" + sPath + "' is not printable!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } opt = _PDFCreator.cOptions; opt.UseAutosave = 1; opt.UseAutosaveDirectory = 1; opt.AutosaveDirectory = Environment.CurrentDirectory + "\\Content\\" + DateTime.Now.Year.ToString(); //fi.DirectoryName; opt.AutosaveFormat = 0; opt.AutosaveFilename = fname; _PDFCreator.cOptions = opt; _PDFCreator.cClearCache(); DefaultPrinter = _PDFCreator.cDefaultPrinter; _PDFCreator.cDefaultPrinter = "PDFCreator"; _PDFCreator.cPrintFile(sPath); ReadyState = false; _PDFCreator.cPrinterStop = false; timer2.Interval = maxTime * 1000; timer2.Enabled = true; while (!ReadyState && timer2.Enabled) { Application.DoEvents(); } timer2.Enabled = false; if (!ReadyState) { MessageBox.Show("Creating printer test page as pdf.\n\r\n\r" + "An error is occured: Time is up!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } _PDFCreator.cPrinterStop = true; _PDFCreator.cDefaultPrinter = DefaultPrinter; ClosePDF(); }
Partager