// 当我走到下面红色的那一段代码的时候就会抛出一个错误信息:“Word could not fire the event.” ,找了好久都没有找出来错误原因在哪里,大家谁找到错误原因是什么吗??
我已经添加了引用->COM->Microsoft Word 11.0 Object Library
代码如下:
using Microsoft.Office.Interop.Word;
public void GenerateExcellenceDoc()
{
string wordmessage = "";
Object Nothing = System.Reflection.Missing.Value;
Directory.CreateDirectory("C:/GenerateAwardDoc");
string name = "AwardNomination_Restricted_" + DateTime.Now.ToString("yyyymmddhhmmss") + ".doc";
object fileName = "C://GenerateAwardDoc//" + name;
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
try
{
Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
WordApp.Visible = true;
WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("RESTRICTED");
WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
WordApp.Selection.ParagraphFormat.LineSpacing = 15f;
object count = 14;
object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;
WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);
WordApp.Selection.TypeParagraph();
WordDoc.Paragraphs.Last.Alignment = WdParagraphAlignment.wdAlignParagraphLeft;
WordDoc.Paragraphs.Last.Range.Text = "INFORMATION SHEET Annex D";
WordDoc.SaveAs(ref fileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
wordmessage = name + "SAVE CHENG GONG!";
}
catch (Exception ex)
{
throw ex;
}
finally
{
WordApp.Documents.Close(ref Nothing, ref Nothing, ref Nothing);
WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}
lblMessage.Text = wordmessage;
ShowHRConnectModalPopUp();
}
浙公网安备 33010602011771号