public static void AutomateWord() { object missing = Type.Missing; object notTrue = false; Word.Application oWord = null; Word.Documents oDocs = null; Word.Document oDoc = null; Word.Paragraphs oParas = null; Word.Paragraph oPara = null; Word.Range oParaRng = null; Word.Font oFont = null; try { // 创建一个Microsoft Word实例并令其不可见 oWord = new Word.Application(); oWord.Visible = false; Console.WriteLine("Word.Application is started"); // 创建一个新的文档 oDocs = oWord.Documents; oDoc = oDocs.Add(ref missing, ref missing, ref missing, ref missing); Console.WriteLine("A new document is created"); // 插入段落 Console.WriteLine("Insert a paragraph"); oParas = oDoc.Paragraphs; oPara = oParas.Add(ref missing); oParaRng = oPara.Range; oParaRng.Text = "Heading 1"; oFont = oParaRng.Font; oFont.Bold = 1; oParaRng.InsertParagraphAfter(); // 将文档保存为.docx文件并关闭 Console.WriteLine("Save and close the document"); object fileName = Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location) + "\\Sample1.docx"; object fileFormat = Word.WdSaveFormat.wdFormatXMLDocument; oDoc.SaveAs(ref fileName, ref fileFormat, 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); ((Word._Document)oDoc).Close(ref missing, ref missing, ref missing); // 退出Word应用程序 Console.WriteLine("Quit the Word application"); ((Word._Application)oWord).Quit(ref notTrue, ref missing, ref missing); } catch (Exception ex) { Console.WriteLine("Solution1.AutomateWord throws the error: {0}", ex.Message); } finally { // 通过在所有访问对象上显示调用Marshal.FinalReleaseComObject方法 // 释放非托管Word COM资源 // 见 http://support.microsoft.com/kb/317109. if (oFont != null) { Marshal.FinalReleaseComObject(oFont); oFont = null; } if (oParaRng != null) { Marshal.FinalReleaseComObject(oParaRng); oParaRng = null; } if (oPara != null) { Marshal.FinalReleaseComObject(oPara); oPara = null; } if (oParas != null) { Marshal.FinalReleaseComObject(oParas); oParas = null; } if (oDoc != null) { Marshal.FinalReleaseComObject(oDoc); oDoc = null; } if (oDocs != null) { Marshal.FinalReleaseComObject(oDocs); oDocs = null; } if (oWord != null) { Marshal.FinalReleaseComObject(oWord); oWord = null; } } }
public static void AutomateWord() { object missing = Type.Missing; object notTrue = false; Word.Application oWord = null; Word.Documents oDocs = null; Word.Document oDoc = null; Word.Paragraphs oParas = null; Word.Paragraph oPara = null; Word.Range oParaRng = null; Word.Font oFont = null; try { // Create an instance of Microsoft Word and make it invisible. oWord = new Word.Application(); oWord.Visible = false; Console.WriteLine("Word.Application is started"); // Create a new Document and add it to document collection. oDoc = oWord.Documents.Add(ref missing, ref missing, ref missing, ref missing); Console.WriteLine("A new document is created"); // Insert a paragraph. Console.WriteLine("Insert a paragraph"); oParas = oDoc.Paragraphs; oPara = oParas.Add(ref missing); oParaRng = oPara.Range; oParaRng.Text = "Heading 1"; oFont = oParaRng.Font; oFont.Bold = 1; oParaRng.InsertParagraphAfter(); // Save the document as a docx file and close it. Console.WriteLine("Save and close the document"); object fileName = Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location) + "\\Sample1.docx"; object fileFormat = Word.WdSaveFormat.wdFormatXMLDocument; // Saves the document with a new name or format. // Some of the arguments for this method correspond to // the options in the Save As dialog box. // For details,please refer to // :http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.saveas(VS.80).aspx oDoc.SaveAs(ref fileName, ref fileFormat, 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); ((Word._Document)oDoc).Close(ref missing, ref missing, ref missing); // Quit the Word application. Console.WriteLine("Quit the Word application"); ((Word._Application)oWord).Quit(ref notTrue, ref missing, ref missing); } catch (Exception ex) { Console.WriteLine("Solution1.AutomateWord throws the error: {0}", ex.Message); } finally { // Clean up the unmanaged Word COM resources by explicitly // calling Marshal.FinalReleaseComObject on all accessor objects. // See http://support.microsoft.com/kb/317109. if (oFont != null) { Marshal.FinalReleaseComObject(oFont); oFont = null; } if (oParaRng != null) { Marshal.FinalReleaseComObject(oParaRng); oParaRng = null; } if (oPara != null) { Marshal.FinalReleaseComObject(oPara); oPara = null; } if (oParas != null) { Marshal.FinalReleaseComObject(oParas); oParas = null; } if (oDoc != null) { Marshal.FinalReleaseComObject(oDoc); oDoc = null; } if (oDocs != null) { Marshal.FinalReleaseComObject(oDocs); oDocs = null; } if (oWord != null) { Marshal.FinalReleaseComObject(oWord); oWord = null; } } }