Automatisation de Word avec c#

alerte001 Messages postés 1 Date d'inscription mercredi 27 juillet 2005 Statut Membre Dernière intervention 27 juillet 2005 - 27 juil. 2005 à 20:43
cs_akhenaton Messages postés 18 Date d'inscription mardi 9 avril 2002 Statut Membre Dernière intervention 10 juin 2008 - 29 juil. 2008 à 10:15
Jessaies uniquement douvrir un document WORD et dy inserer un paragraphe
Jutilise Word 2002 (office XP)
Jai ajoute la reference a Word a mon projet


Pbme: quand jouvre le document word il copie tout le contenu de mon interface(cest a dire tous mes labels, bouton etc...); est ce que qqun a deja rencontre ce pbme et comment le resoudre?
aussi je ne suis pas sur de la maniere que la methode saveAs de Word fonctionne; qqun pourrait meclaircir les idees?
Merci davance


Code: il y a deux classes dans le code ci-dessous; jappelle WordWriter.cs dans Recherche.aspx.cs



//classe WordWriter.cs
using System;
//using System.Threading;
//using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using Microsoft.Office.Interop.Word;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Data.SqlClient;
using System.Collections;
using System.Text.RegularExpressions;
using System.Reflection;


namespace SOLROC
{
public class WordWriter
{
private Microsoft.Office.Interop.Word.ApplicationClass oWordApp;
private Microsoft.Office.Interop.Word.Documents oDocs;
private Microsoft.Office.Interop.Word.Document oDoc;
private Microsoft.Office.Interop.Word.Paragraph oPara1;


private object oRien;
private object strTemplateARetourner;
private object strTemplate;


//template a ouvrir
public object ReportTemplate
{
get { return strTemplate; }
set { strTemplate = value; }
}


//template quon utilisera pour sauvegarder nos resultats
public object DocARetourner
{
get { return strTemplateARetourner; }
set { strTemplateARetourner = value; }
}


public WordWriter()
{
oWordApp = null;
oDocs = null;
oDoc = null;
oPara1 = null;

oRien = System.Reflection.Missing.Value;
Index = 1;
}


//methode pour ecriture dans TemplateLabels.doc
public bool GetRapport ()
{
try
{

OuvrirTemplate();


if (File.Exists((string)strTemplateARetourner))
File.Delete((string)strTemplateARetourner);



oPara1.Range.Text = "Header 1";
oPara1.Range.Font.Bold = 1;
oPara1.Range.Font.Size = 12;
oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.

oPara1.Range.InsertParagraphAfter();


oDoc.SaveAs(ref strTemplateARetourner, ref oRien, ref oRien, ref oRien,
ref oRien, ref oRien, ref oRien, ref oRien, ref oRien,
ref oRien,ref oRien, ref oRien, ref oRien, ref oRien,
ref oRien, ref oRien);
FermerTemplate();


return true;
}
catch (Exception)
{
}
finally
{
}
return false;
}


public void OuvrirTemplate()
{
if (oWordApp != null)
FermerTemplate();


oWordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
oDocs = oWordApp.Documents;

oDoc = oDocs.Open(ref strTemplate, ref oRien,
ref oRien, ref oRien, ref oRien, ref oRien,
ref oRien, ref oRien, ref oRien, ref oRien,
ref oRien, ref oRien, ref oRien, ref oRien,
ref oRien);
oDoc.Activate();
oPara1 = oDoc.Content.Paragraphs.Add(ref oRien);
}


public void FermerTemplate()
{
//Quitter Excel et nettoyer les Objets
if (oPara1 != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(oPara1);
oPara1 = null;
if (oDoc != null) oDoc.Close(ref oRien, ref strTemplate, ref oRien);
if (oDoc != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(oDoc);
oDoc = null;
if (oDocs != null) System.Runtime.InteropServices.Marshal.ReleaseComObject(oDocs);
oDocs = null;

if (oWordApp != null)
{
oWordApp.Quit(ref oRien, ref oRien, ref oRien);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWordApp);
oWordApp = null;
}
//appel du garbage collector
System.GC.Collect();
}


}
}



//classe Recherche.aspx.cs


using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Reflection;
using System.Collections;


namespace SOLROC
{
public class Recherche_aspx : System.Web.UI.Page
{
//methode appellee quand on clique sur un ImageButton
protected void printWord(Object sender, ImageClickEventArgs e)
{
try
{
WordWriter wd = new WordWriter();
wd.ReportTemplate =
Server.MapPath("Template") +"[file://TemplateLabels.doc/ \\TemplateLabels.doc]";
wd.DocARetourner =
Server.MapPath("RapportExcel") + "[file://TemplateLabels.doc/ \\TemplateLabels.doc]";
wd.Cache = Server.MapPath("Cache");
//methode qui ouvre le document ReportTemplate et le sauvegarde
//en tant que DocARetourner
wd.GetRapport();
Response.Clear();
Response.ContentType = "application/x-msword";
string nomfic = "Labels.doc";
Response.AddHeader("Content-Disposition", "inline; filename=" + nomfic +";");
Response.WriteFile((string)wd.DocARetourner);

//JavaScriptPourErreur.Text = "javascript:printpage()";
}
catch (Exception exec)
{
//linkReinitialiseRecherche.Text = exec.ToString();
}


}
}
}

1 réponse

cs_akhenaton Messages postés 18 Date d'inscription mardi 9 avril 2002 Statut Membre Dernière intervention 10 juin 2008
29 juil. 2008 à 10:15
Bonjour ,

jab.developpez.com/CSharp/Word/



je pense que ça va t'aider.

très cordialement
0
Rejoignez-nous