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.
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.
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.
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With