【问题标题】:Open files in Word via ribbon code-behind通过功能区代码隐藏在 Word 中打开文件
【发布时间】:2012-12-07 04:21:32
【问题描述】:

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

但是我怎样才能真正将这些文档加载到 Word 中呢?我已经在 Word 中的代码隐藏中,那么如何定位我所在的 Word 实例并在那里打开文件?

提前致谢。

【问题讨论】:

    标签: c# sharepoint ms-word vsto ribbon


    【解决方案1】:

    您必须使用 Word 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();
        }
    }
    

    【讨论】:

    • 是的,这就是我现在的工作。所以它有点工作,但我有一个问题......它在一个新的 Word 窗口中打开,而不是我最初使用的实例。有没有办法“解决”这个问题?
    • 我在这里找到了答案:social.msdn.microsoft.com/Forums/en-US/vsto/thread/…。不得不使用 Globals.ThisAddin.Application.Documents.Open(...)
    猜你喜欢
    • 1970-01-01
    • 2012-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 2021-07-07
    • 2012-11-30
    • 2011-11-07
    相关资源
    最近更新 更多