views:

542

answers:

1

Using VSTO, I've created a custom tab in the Ribbon designer and added some groups and button controls there. When user clicks one of the buttons, I'd like to connect to a SharePoint site and open a word document from it in Word (an instance is already open). I'm able to connect to the SharePoint site already and have the URLs to the documents I want to open.

But how can I actually load these documents into Word? I'm already in the code-behind in Word, so how can I target the Word instance I'm in and open a file there?

Thanks in advance.

+5  A: 

You would have to use the Word API to open a document. See this link for a reference. You may have to update it based on the API version you use.

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();
    }
}
AboutDev
Yeah, that's what I've got working now. So it kind of works, but I have an issues with this... it opens in a new Word window, not the instance I was initially using. Is there a way 'fix' that?
Kon
I found my answer here: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/b6fa2787-bf87-4ef2-9c99-9df9f2c0a202/. Had to use Globals.ThisAddin.Application.Documents.Open(...)
Kon