调用Word for rtf到docx转换

Gre*_*egA 6 .net c# rtf ms-word office-interop

我需要定期以编程方式将*.rtf文件转换为*.docx.手动,这可以正常使用Word 2007中的另存为...得到的docx表现得很好.以编程方式,我无法让它工作.

我尝试的基本上如下:

从Word获取RTF

......但反方向.我打开*.rtf并使用SaveAs到*.docx,而不是打开*.docx并使用SaveAs到*.rtf.但是,生成的文件不会打开,显然有些东西我不明白.是

wordApp.Documents.Open(@"D:\Bar\foo.rtf")
Run Code Online (Sandbox Code Playgroud)

不是一个合法的事情吗?

任何关于如何做到这一点的想法将不胜感激.

Ahm*_*gdy 4

你可以试试这个代码,它对我有用

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);
Run Code Online (Sandbox Code Playgroud)

当我尝试使用 wdFormatDocument 或 wdFormatDocumentDefault 时,出现同样的错误

编辑:这是对代码的更新,它会对其进行转换,但您会收到错误一次,然后它就再也不会出现了!

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();
Run Code Online (Sandbox Code Playgroud)