如何使用 C# 打开 RTF 文件而不显示 Microsoft Word“转换文件”对话?

发布于 2024-10-17 16:02:36 字数 3146 浏览 0 评论 0原文

问题

我想使用 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);

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小猫一只 2024-10-24 16:02:36

根据 Open 的文档方法,第二个参数(ConfirmConversions)可以指定禁用文档转换。将它与 (ReadOnly 参数) 结合起来,它应该可以解决您的问题。

bool f = false;
bool t = true;
word.Documents.Open(ref fullFilePath, ref t, ref f, 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);

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.

bool f = false;
bool t = true;
word.Documents.Open(ref fullFilePath, ref t, ref f, 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文