Exemplo n.º 1
0
        public static void WordToHtml(string sourceFileName, string targetFileName)
        {
            Microsoft.Office.Interop.Word.ApplicationClass WordApp;
            Microsoft.Office.Interop.Word.Document         WordDoc;
            Object oMissing = System.Reflection.Missing.Value;

            WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
            object fileName = sourceFileName;

            WordDoc = WordApp.Documents.Open(ref fileName,
                                             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                                             ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            try
            {
                Type wordType = WordApp.GetType();
                // 打开文件
                Type docsType = WordApp.Documents.GetType();
                // 转换格式,另存为
                Type   docType      = WordDoc.GetType();
                object saveFileName = targetFileName;
                docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, WordDoc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML });
            }
            finally
            {
                WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
            }
        }