3

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

1 回答 1

4

根据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);
于 2011-02-16T13:11:19.573 回答