代码之家  ›  专栏  ›  技术社区  ›  Kon

通过后面的功能区代码打开Word中的文件

  •  6
  • Kon  · 技术社区  · 16 年前

    使用vsto,我在功能区设计器中创建了一个自定义选项卡,并在其中添加了一些组和按钮控件。当用户单击其中一个按钮时,我想连接到一个SharePoint网站,并在Word中打开一个Word文档(实例已打开)。我已经可以连接到SharePoint网站,并拥有我要打开的文档的URL。

    但我如何才能将这些文档实际加载到Word中呢?我已经在word后面的代码中了,那么我如何以我所在的word实例为目标并在那里打开一个文件呢?

    事先谢谢。

    1 回复  |  直到 12 年前
        1
  •  7
  •   AboutDev    16 年前

    您必须使用单词api打开文档。看到这个 link 作为参考。您可能需要根据所使用的API版本对其进行更新。

    private void button1_Click(object sender, System.EventArgs e)
    {
        // Use the open file dialog to choose a word document
        if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            // set the file name from the open file dialog
            object fileName = openFileDialog1.FileName;
            object readOnly = false;
            object isVisible = true;
            // Here is the way to handle parameters you don't care about in .NET
            object missing = System.Reflection.Missing.Value;
            // Make word visible, so you can see what's happening
            WordApp.Visible = true;
            // Open the document that was chosen by the dialog
            Word.Document aDoc = WordApp.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible);
            // Activate the document so it shows up in front
            aDoc.Activate();
            // Add the copyright text and a line break
            WordApp.Selection.TypeText("Copyright C# Corner");
            WordApp.Selection.TypeParagraph();
        }
    }