调用 Word 进行 rtf 到 docx 的转换 [英] Invoking Word for rtf to docx conversion

查看:24
本文介绍了调用 Word 进行 rtf 到 docx 的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要定期以编程方式将 *.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.

我尝试的基本上如下:

从 Word 中获取 RTF

...但方向相反.我没有打开 *.docx 并使用 SaveAs 到 *.rtf,而是打开 *.rtf 并使用 SaveAs 到 *.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:Barfoo.rtf")

这不合法吗?

如有任何关于如何执行此操作的想法,我们将不胜感激.

Any thoughts about how to do this would be appreciated.

推荐答案

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

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

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

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆