Automation Word Windows Server 2003

cs_2bo Messages postés 3 Date d'inscription lundi 18 juin 2007 Statut Membre Dernière intervention 22 juillet 2009 - 22 juil. 2009 à 11:29
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 - 26 juil. 2009 à 12:12
Bonjour,

je développe une application qui produit des documents word et excel. Lorsque j'essaye de créer mes documents excel je n'ai aucun problème, par contre pour mes documents word je ne parviens à les créer qu'en environnement de développement.

Lorsque je dépose mon application sur le serveur (Windows Server 2003) si j'essaye de créer mes documents word j'ai une exception qui est lancée sans aucun détail.

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 388: catch (Exception err)
Line 389: {
Line 390: throw (err);
Line 391: Trace.Write(err.Message);
Line 392: }


Source File: d:\Inetpub\wwwroot\PMPRO\relanceRisque.aspx.cs Line: 390

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
relanceRisque.ButtonEnvoiMail_Click(Object sender, EventArgs e) in d:\Inetpub\wwwroot\PMPRO\relanceRisque.aspx.cs:390
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +107
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

Alors qu'en local tout se passe correctement.

J'ai donné les droits d'exécution à l'utilisateur ASPNET et à SERVICE RESEAU. Mais le problème ne semble pas venir de là. Sur le serveur, word se lance mais je ne peux faire aucune opération dessus.

Je suppose qu'il s'agit d'un problème de paramètrage de windows server 2003. Je sais que ce n'est pas conseillé d'utiliser l'automation d'office côté server mais si je parviens à le faire fonctionner ça me ferait gagner beaucoup de temps.

C'est l'ouverture du document qui ne fonctionne pas.

oDoc = oWord.Documents.Add(ref oTemplate, ref oMissing,
ref oMissing, ref oMissing);

oDoc vaut null dans cette ligne. Et donc la suite des opérations ne peut pas se dérouler correctement.

Merci d'avance

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;
}

1 réponse

cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
26 juil. 2009 à 12:12
Bonjour,


A creuser, entre autres :
- utiliser un autre compte que ASPNET pour l'application Web, afin de pouvoir lancer Word sous ce compte, faire les étapes d'install à la première utilisation etc etc etc
- vérifier que le compte utilisé a accès à au moins une imprimante, de mémoire certaines actions ne marchent pas si Word n'a accès à aucune imprimante (si non, en installer une bidon, type texte vers fichier et la définir par défaut)


/*
coq
MVP Visual C#
CoqBlog
*/
0
Rejoignez-nous