Visual Studio Tools for Office を使用してプログラムで新しい Word ドキュメントを作成するにはどうすればよいですか?
3 に答える
実際に求めているのは、PIA (Primary Interop Assemblies) を使用した Office オートメーションです。
VSTO は実際にはマネージド .net 拡張機能のセットであり、Office 用のアドインの作成をはるかに簡単にします。外部とのやり取りでは、VSTO はまったく使用されません (必要に応じて、VSTO ライブラリを参照したり、一部のヘルパーを使用したりすることはできますが)。
開始するには、http://support.microsoft.com/kb/316384をご覧ください。Google で「word interop create document」
VSTOアプリレベルのアドインの場合、次のように実行できます。
Globals.ThisAddIn.Application.Documents.Add(ref objTemplate, ref missingType, ref missingType, ref missingType);
objTemplate
ドキュメントのテンプレートはどこにありますか
Documents.Addメソッドを参照してください
これについては間違っているかもしれませんが、VSTO を使用して新しい Word ドキュメントを実際に作成できるとは思えません。私は VSTO に詳しくないので、その点で間違っていたらすみません。
ただし、Office Interop ライブラリを使用してこれを行うことができることは知っています。
ライブラリをダウンロードするには、必要な Office バージョンを含む可能性がある "office interop assembly" を検索してください (例: "office interop assembly 2007")。
Word Interop アセンブリを ([参照の追加] を使用して) アプリケーションに含めたら、次のようなことができます。
using Word = Microsoft.Office.Interop.Word;
object missing = System.Reflection.Missing.Value;
Word.Application app = new Word.ApplicationClass();
Word.Document doc = app.Documents.Add(ref missing, ref missing, ref missing, ref missing);
doc.Activate();
app.Selection.TypeText("This is some text in my new Word document.");
app.Selection.TypeParagraph();
それが役立つことを願っています!