2009-02-13 100 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