调用Word的RTF到DOCX转换 [英] Invoking Word for rtf to docx conversion
问题描述
我有一个需要经常编程转换* .rtf文件成* .DOCX。手动,这工作得很好用另存为里面的Word 2007年......由此产生的docx的行为就好。编程,我不能让它的工作。
I have a need to routinely programmatically convert *.rtf files into *.docx. Manually, this works just fine with Save As inside Word 2007 ... the resulting docx behaves just fine. Programmatically, I can't get it to work.
我试过基本上是以下内容:
What I tried is basically the following:
从Word 获取RTF
...但在相反的方向。相反,开* .DOCX和使用另存为* .RTF,我打开* .RTF和使用另存为* .DOCX。但是,生成的文件将无法打开,因此显然有什么东西我不明白。为
... but in the reverse direction. Instead of opening *.docx and using SaveAs to *.rtf, I'm opening the *.rtf and using SaveAs to *.docx. However, the resulting file won't open, and so evidently there's something I don't understand. Is
wordApp.Documents.Open(@"D:\Bar\foo.rtf")
不是一个合法的事是什么?
not a legit thing to do?
有关如何执行这将是AP preciated任何想法。
Any thoughts about how to do this would be appreciated.
推荐答案
您可以试试这个code,它为我的作品
You can try this code, it works for me
var wordApp = new Microsoft.Office.Interop.Word.Application();
var currentDoc = wordApp.Documents.Open(@"C:\TestDocument.rtf");
currentDoc.SaveAs(@"C:\TestDocument.doc", Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument97);
我得到了同样的错误,当我尝试使用于wdFormatDocument或wdFormatDocumentDefault
I got the same error when I tried to use wdFormatDocument or wdFormatDocumentDefault
编辑:这是一个更新到code,将其转换,但U将得到错误一次,然后就再也没有出现过一次!
this is an update to the code, it converts it but u will get the error once then it never appeared again!!
var wordApp = new Microsoft.Office.Interop.Word.Application();
var currentDoc = wordApp.Documents.Open(@"C:\TestDocument.rtf");
currentDoc.SaveAs(@"C:\TestDocument", Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault);
currentDoc.Close();
wordApp.Quit();
这篇关于调用Word的RTF到DOCX转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!