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
| using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
namespace testword
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
object field;
Microsoft.Office.Interop.Word.Application msWord = new Microsoft.Office.Interop.Word.Application();
msWord.Visible = true;
object missing = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Word.Document nvDoc;
// Choisir le template
object templateName = @"C:\Users\onganme\Desktop\testword\testword\testword\bin\Debug\Facture.docx";
// Créer le document
nvDoc = msWord.Documents.Add(ref templateName, ref missing, ref missing, ref missing);
field = "nomClient";
nvDoc.FormFields.get_Item(ref field).Result = "Ongan Mehmet";
field = "adresse";
nvDoc.FormFields.get_Item(ref field).Result = "Grande Rue";
field = "villeNpa";
nvDoc.FormFields.get_Item(ref field).Result = "2000 Neuchâtel";
// Attribuer le nom
object fileName = @"C:\Users\onganme\Desktop\testword\testword\testword\bin\Debug\Facture_client.doc";
// Sauver le document
nvDoc.SaveAs(fileName);
nvDoc.Close(ref missing, ref missing, ref missing);
msWord.Quit(ref missing, ref missing, ref missing);
}
}
} |