6

VSTOを使用して、リボンデザイナーでカスタムタブを作成し、そこにいくつかのグループとボタンコントロールを追加しました。ユーザーがボタンの1つをクリックしたら、SharePointサイトに接続し、そこからWord文書をWordで開きます(インスタンスは既に開いています)。すでにSharePointサイトに接続して、開きたいドキュメントのURLを取得できました。

しかし、どうすればこれらのドキュメントを実際にWordにロードできますか?私はすでにWordのコードビハインドに参加していますが、参加しているWordインスタンスをターゲットにして、そこでファイルを開くにはどうすればよいですか?

前もって感謝します。

4

1 に答える 1

7

ドキュメントを開くには、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();
    }
}
于 2009-02-13T20:22:31.857 に答える