使用VSTO,我在功能区设计器中创建了一个自定义选项卡,并在那里添加了一些组和按钮控件。当用户单击其中一个按钮时,我想连接到SharePoint站点并在Word中打开Word文档(实例已打开)。我已经能够连接到SharePoint站点并拥有我想要打开的文档的URL。
但是我怎样才能将这些文档加载到Word中?我已经在Word中的代码隐藏中,所以如何定位我所在的Word实例并在那里打开文件?
提前致谢。
答案 0 :(得分:7)
您必须使用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();
}
}