gpt4 book ai didi

c# - 调用 Word 进行 rtf 到 docx 的转换

转载 作者:太空狗 更新时间:2023-10-29 21:36:29 26 4
gpt4 key购买 nike

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

我试过的基本上是这样的:

Fetch RTF from Word

...但方向相反。我没有打开 *.docx 并使用另存为 *.rtf,而是打开 *.rtf 并使用另存为 *.docx。但是,生成的文件不会打开,所以显然有一些我不明白的地方。是

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

不是合法的事情吗?

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

最佳答案

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

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

关于c# - 调用 Word 进行 rtf 到 docx 的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4903304/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com