2011-01-10 58 views
1

我有一個Word文檔,並希望導出格式爲RTF(或html)的內容。從Word獲取RTF

 Word.Application wordApp = new Word.Application(); 
     Word.Document currentDoc = wordApp.Documents.Open("file.docx"); 
     currentDoc.SaveAs("file.rtf", Word.WdSaveFormat.wdFormatRTF); 
     currentDoc = wordApp.Documents.Open("file.rtf"); 
     Word.Range range = currentDoc.Range(); 
     String RTFText = range.Text; 

我試過上面的代碼,但我似乎只是得到文本,沒有hte格式。

任何想法?

回答

3

如果你想讀的RTF代碼只是嘗試使用:

Word.Application wordApp = new Word.Application(); 
Word.Document currentDoc = wordApp.Documents.Open("file.docx"); 
currentDoc.SaveAs("file.rtf", Word.WdSaveFormat.wdFormatRTF); 

,然後打開它就像一個普通的文本文件:

string rtf = File.ReadAllText("file.rtf"); 

使用你的方法不會因爲你的工作'重新訪問文本屬性,所以Word只給你純文本。

+0

我在想辦法複雜。謝謝! – Jaster 2011-01-10 14:07:09