如何使用 C# 打开 RTF 文件而不显示 Microsoft Word“转换文件”对话?
问题
我想使用 C# 以编程方式在 Microsoft Word 中打开 RTF 文件。但我不想在执行此操作时出现“转换文件”对话框。我该怎么做?
代码
我已经尝试过这段代码,但它仍然显示Word的“转换文件”对话框。
object missing = Missing.Value;
string fileName = @"C:\RtfFile.rtf";
//object encoding = WdSaveFormat.wdFormatRTF;
object encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;
object noEncodingDialog = true; // http://msdn.microsoft.com/en-us/library/bb216319(office.12).aspx
word.Documents.Open(ref fullFilePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref encoding, ref missing, ref missing, ref missing, ref noEncodingDialog, ref missing);
到目前为止我已经尝试过的
实际上,有两个子问题。
一方面,我不确定使用哪种编码。我已经尝试了代码片段中显示的两个选项,并检查了 维基百科的 RTF 字符编码 。
另一方面,object noEncodingDialog = true
似乎不起作用,因为“转换文件”对话框不断弹出(可能是因为编码设置错误)。
有什么想法吗?
版本
- Word 2003
- .NET 3.5
- Visual Studio 2010 Ultimate
完整源代码
以防万一您想尝试一下。
从“COM”选项卡添加对“Microsoft Word 11.0 对象库”的引用。
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Word;
namespace WordAutomationDemo
{
public class Program
{
static void Main(string[] args)
{
new Program();
Console.ReadKey();
}
public Program()
{
object wordObject = null;
try
{
wordObject = Marshal.GetActiveObject("Word.Application");
}
catch (Exception)
{
// Do nothing.
}
Application word = null;
bool wordInstanceCreated = false;
if (wordObject != null)
{
word = (Application)wordObject;
}
else
{
wordInstanceCreated = true;
word = new Application();
}
word.Visible = true;
object missing = Missing.Value;
object fullFilePath = @"C:\RtfFile.rtf";
//object encoding = WdSaveFormat.wdFormatRTF; // http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.saveas(v=vs.80).aspx
object encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;
object noEncodingDialog = true; // http://msdn.microsoft.com/en-us/library/bb216319(office.12).aspx
word.Documents.Open(ref fullFilePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref encoding, ref missing, ref missing, ref missing, ref noEncodingDialog, ref missing);
//if (wordInstanceCreated)
//{
// word.Quit();
//}
}
}
}
Question
I want to programmatically open a RTF file in Microsoft Word using C#. But I don't want to get the "Convert File" dialog while doing that. How do I do that?
Code
I've tried this piece of code, but it still shows Word's "Convert File" dialog.
object missing = Missing.Value;
string fileName = @"C:\RtfFile.rtf";
//object encoding = WdSaveFormat.wdFormatRTF;
object encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;
object noEncodingDialog = true; // http://msdn.microsoft.com/en-us/library/bb216319(office.12).aspx
word.Documents.Open(ref fullFilePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref encoding, ref missing, ref missing, ref missing, ref noEncodingDialog, ref missing);
What I have tried so far
Actually, there are two sub-questions.
On one hand, I am not sure which encoding to use. I've tried both options shown in the code snippet and checked Wikipedia for RTF character encoding.
On the other hand, object noEncodingDialog = true
doesn't seem to work since the "Convert File" dialog keeps popping up (probably because the encoding is set wrongly).
Any ideas?
Versions
- Word 2003
- .NET 3.5
- Visual Studio 2010 Ultimate
Full source code
Just in case you want to try it out.
Add reference to "Microsoft Word 11.0 Object Library" from the "COM" tab.
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.Word;
namespace WordAutomationDemo
{
public class Program
{
static void Main(string[] args)
{
new Program();
Console.ReadKey();
}
public Program()
{
object wordObject = null;
try
{
wordObject = Marshal.GetActiveObject("Word.Application");
}
catch (Exception)
{
// Do nothing.
}
Application word = null;
bool wordInstanceCreated = false;
if (wordObject != null)
{
word = (Application)wordObject;
}
else
{
wordInstanceCreated = true;
word = new Application();
}
word.Visible = true;
object missing = Missing.Value;
object fullFilePath = @"C:\RtfFile.rtf";
//object encoding = WdSaveFormat.wdFormatRTF; // http://msdn.microsoft.com/en-us/library/microsoft.office.tools.word.document.saveas(v=vs.80).aspx
object encoding = Microsoft.Office.Core.MsoEncoding.msoEncodingUTF8;
object noEncodingDialog = true; // http://msdn.microsoft.com/en-us/library/bb216319(office.12).aspx
word.Documents.Open(ref fullFilePath, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref encoding, ref missing, ref missing, ref missing, ref noEncodingDialog, ref missing);
//if (wordInstanceCreated)
//{
// word.Quit();
//}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据
Open
的文档方法,第二个参数(ConfirmConversions
)可以指定禁用文档转换。将它与 (ReadOnly
参数) 结合起来,它应该可以解决您的问题。According the documentation of the
Open
method, the second argument (ConfirmConversions
) can specify to disable the document conversion. Combining it with the (ReadOnly
argument) and it should solve your problem.