Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open files in Word via ribbon code-behind

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.

like image 282
Kon Avatar asked Feb 13 '09 17:02

Kon


People also ask

What part of the ribbon do you go to in order to save a file?

To save a document: Locate and select the Save command on the Quick Access Toolbar. If you're saving the file for the first time, the Save As pane will appear in Backstage view.

What is the File tab in Word?

The File tab is a colored tab, for example, a blue tab in Word, located in the upper-left corner of Microsoft Office programs. When you click the File tab, you see many of the same basic commands that you saw on the File menu in earlier releases of Microsoft Office, such as Open, Save, and Print.


1 Answers

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();
    }
}
like image 153
AboutDev Avatar answered Oct 16 '22 10:10

AboutDev