首页 文章

调用Word for rtf到docx转换

提问于
浏览
6

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

我尝试的基本上如下:

Fetch RTF from Word

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

wordApp.Documents.Open(@"D:\Bar\foo.rtf")

不是一个合法的事情吗?

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

3 回答

  • 0

    你可以尝试这个代码,它适合我

    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时,我遇到了同样的错误

    编辑:这是代码的更新,它转换它但你会得到错误一次然后它再也没有出现!

    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();
    
  • 2

    你能显示你所在的代码 SaveAs 吗?我很好奇你指定的是 Word.WdSaveFormat . 听起来它正在保存rtf数据,但将扩展名更改为.docx .

  • 4

    这是进行转换的代码 . 代码几乎与上面显示的相同,但有一些小的(但很重要的)差异 - 必须使用引用(而不是对象本身):

    Microsoft.Office.Interop.Word.Application _App = new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Interop.Word.Document _Doc =  _App.Documents.Open("c:/xxx.rtf");
    
    object _DocxFileName = "C:/xxx.docx";
    Object FileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLDocument;
    
    _Doc.SaveAs2(ref _DocxFileName, ref FileFormat);
    

相关问题