
使用wordapi打开word文档.docx
2页杭州java软件培训 使用Word API(非Openxml)打开Word文档简单示例(必须安装Word)1. 首先需要引入参照Microsoft.Office.Interop.Word2. 代码示例 1 public void OpenWord() 2 { 3 // Word应用对象 4 Word.Application wdApp = null; 5 // Word文档对象 6 Word.Document wdDoc = null; 7 8 // Word路径 9 object oWdPath = "XXXXXXXXXXXXXX";10 // Word设定:缺损项11 object oMissing = System.Reflection.Missing.Value;12 // Word设定:不保存13 object doNotSaveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;14 15 try16 {17 // 新建Word应用18 wdApp = new Word.Application();19 // 设置Word应用为可见20 wdApp.Visible = true;21 // 打开Word文档22 wdDoc = wdApp.Documents.Open(ref oWdPath,23 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,24 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,25 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);26 }27 finally28 {29 #pragma warning disable30 // 关闭Word文档31 if (wdDoc != null) { wdDoc.Close(); wdDoc = null; }32 // 关闭Word应用33 if (wdApp != null) { wdApp.Quit(ref doNotSaveChanges, ref oMissing, ref oMissing); wdApp = null; }34 #pragma warning disable35 GC.Collect();36 }37 }杭州网络营销培训 。












