リボンコードビハインドを経由してWordで開いているファイル
-
23-08-2019 - |
質問
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();
}
}
所属していません StackOverflow