gpt4 book ai didi

c# - 通过功能区代码隐藏在 Word 中打开文件

转载 作者:太空狗 更新时间:2023-10-29 21:31:45 25 4
gpt4 key购买 nike

使用 VSTO,我在功能区设计器中创建了一个自定义选项卡,并在其中添加了一些组和按钮控件。当用户单击其中一个按钮时,我想连接到 SharePoint 站点并在 Word 中从中打开一个 word 文档(一个实例已经打开)。我已经能够连接到 SharePoint 网站并获得我想要打开的文档的 URL。

但是我怎样才能真正将这些文档加载到 Word 中呢?我已经在 Word 的代码隐藏中,那么如何定位我所在的 Word 实例并在那里打开文件?

提前致谢。

最佳答案

您必须使用 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();
}
}

关于c# - 通过功能区代码隐藏在 Word 中打开文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/546961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com