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