如何通过Interop打开文档时使Word可见?

8

我想通过Interop打开一个Word文档,并且在过程中必须使Word可见。这似乎很简单,因为在Word文档的“open”函数中有一个名为“visible”的参数。但是Word在后台运行。我错过了什么?

static void Main(string[] args)
{
    Microsoft.Office.Interop.Word.Application word = null;
    word = new Microsoft.Office.Interop.Word.Application();

    object inputFile = "c:\\test.docx";
    object confirmConversions = false;
    object readOnly = true;
    object visible = true;
    object missing = Type.Missing;

    // Open the document...
    Microsoft.Office.Interop.Word.Document doc = null;
    doc = word.Documents.Open(
        ref inputFile, ref confirmConversions, ref readOnly, ref missing,
        ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref visible,
        ref missing, ref missing, ref missing, ref missing);
    doc.Activate();

    Console.ReadKey();
}

2
如果你正在使用C# 4.0,考虑使用dynamic。http://www.devx.com/dotnet/Article/42590 - Lukasz Madon
谢谢。这非常有启发性。 - Kasper Hansen
1个回答

8

嗯。显然应用程序和文档都必须可见。所以解决方法是在doc.Activate()之前添加以下行:

word.Visible = true;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接