开发者

How to open a RTF file with C# without showing the Microsoft Word "Convert File" dialog?

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 mis开发者_如何学JAVAsing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref encoding, ref missing, ref missing, ref missing, ref noEncodingDialog, ref missing);

How to open a RTF file with C# without showing the Microsoft Word "Convert File" dialog?

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜