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
| public static bool docRisques(string redacteur, string destinataire, string validateur, string copie, string tel, string sujet, string corpsDeb, string corpsFin, string date, string path, DataTable info)
{
bool result = false;
object missing = System.Reflection.Missing.Value;
object oMissing = System.Reflection.Missing.Value;
object oEndOfDoc = "\\endofdoc"; /* \endofdoc is a predefined bookmark */
object fileName = path + @"Documents\Relance.doc";
//Start Word and create a new document.
Word._Application oWord;
Word._Document oDoc;
oWord = new Word.Application();
oWord.Visible = true;
object oTemplate = path + @"Documents\relanceRisque.dot";
oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
ref oMissing, ref oMissing);
try
{
// Ajout de texte sur un signet
object cible = "Emetteur";
oDoc.Bookmarks.get_Item(ref cible).Range.Text = redacteur;<--- Cette instruction lève l'exception
cible = "Destinataire";
oDoc.Bookmarks.get_Item(ref cible).Range.Text = destinataire;
cible = "Validateur";
oDoc.Bookmarks.get_Item(ref cible).Range.Text = validateur;
cible = "Copie";
oDoc.Bookmarks.get_Item(ref cible).Range.Text = copie;
cible = "Tel";
oDoc.Bookmarks.get_Item(ref cible).Range.Text = tel;
cible = "Objet";
oDoc.Bookmarks.get_Item(ref cible).Range.Text = sujet;
cible = "Corps";
oDoc.Bookmarks.get_Item(ref cible).Range.Text = corpsDeb;
cible = "DateNote";
oDoc.Bookmarks.get_Item(ref cible).Range.Text = date;
int nbLigne = info.Rows.Count;
// Création d'une table et insertion des données
Word.Table oTable;
cible = "DocsManquants";
Word.Range wrdRng = oDoc.Bookmarks.get_Item(ref cible).Range;
oTable = oDoc.Tables.Add(wrdRng, nbLigne + 1, 7, ref oMissing, ref oMissing);
oTable.Range.ParagraphFormat.SpaceAfter = 6;
oTable.Cell(1, 1).Range.Text = "Ouverture";
oTable.Cell(1, 2).Range.Text = "N° de Compte";
oTable.Cell(1, 3).Range.Text = "Intitulé client";
oTable.Cell(1, 4).Range.Text = "Conseiller";
oTable.Cell(1, 5).Range.Text = "N° Bureau";
oTable.Cell(1, 6).Range.Text = "Bureau";
oTable.Cell(1, 7).Range.Text = "Groupe";
oTable.Rows[1].Range.Font.Bold = 1;
int r, c;
string strText;
for (r = 1; r <= nbLigne; r++)
{
for (c = 1; c <= 7; c++)
{
switch (c)
{
case 1:
strText = DateTime.Parse(info.Rows[r - 1][c - 1].ToString()).ToShortDateString();
break;
case 5:
Regex regExp = new Regex(@"(\d+)");
MatchCollection theMatches = regExp.Matches(info.Rows[r - 1][c - 1].ToString());
strText = theMatches[0].Groups[1].ToString();
break;
default:
strText = info.Rows[r - 1][c - 1].ToString();
break;
}
oTable.Cell(r + 1, c).Range.Text = strText;
}
oTable.Rows[r + 1].Range.Font.Size = 10;
}
oDoc.Bookmarks.get_Item(ref oEndOfDoc).Range.Text = corpsFin;
// Sauvegarde du document
oDoc.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);
// Fermer le document
oDoc.Close(ref missing, ref missing, ref missing);
result = true;
}
catch (Exception e)
{
throw (e);
}
finally
{
// Fermeture de word
oWord.Quit(ref missing, ref missing, ref missing);
}
return result;
} |
Partager