2009-02-13 51 views
6

使用VSTO,我在功能区设计器中创建了一个自定义选项卡,并在其中添加了一些组和按钮控件。当用户点击其中一个按钮时,我想连接到SharePoint网站并在Word中打开一个Word文档(一个实例已经打开)。我可以连接到SharePoint网站,并具有要打开的文档的URL。通过功能区代码隐藏在Word中打开文件

但我怎样才能将这些文件实际加载到Word中?我已经在Word中的代码隐藏,所以我如何定位我所在的Word实例并在那里打开一个文件?

在此先感谢。

回答

6

您将不得不使用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(); 
    } 
} 
+0

是的,这就是我现在的工作。所以这是有用的,但我有这个问题......它打开一个新的Word窗口,而不是我最初使用的实例。有没有办法“解决”? – Kon 2009-02-13 21:54:45