1

如何使用 Visual Studio Tools for Office 以编程方式创建新的 Word 文档?

4

3 回答 3

5

您真正追求的是使用 PIA(主要互操作程序集)的办公自动化。

VSTO 实际上是一组托管的 .net 扩展,这使得为 Office 编写加载项变得更加容易。对于外部交互,根本不使用 VSTO(尽管您仍然可以参考 VSTO 库并根据需要使用一些帮助程序)。

请查看http://support.microsoft.com/kb/316384以帮助您入门。和谷歌'word interop 创建文档'

于 2011-05-22T23:52:23.343 回答
2

对于 VSTO 应用级加载项,您可以执行以下操作:

Globals.ThisAddIn.Application.Documents.Add(ref objTemplate, ref missingType, ref missingType, ref missingType); 

哪里objTemplate可以是文档模板

请参阅Documents.Add 方法

于 2008-11-18T12:30:37.933 回答
0

现在,我可能错了,但我不相信你真的可以使用 VSTO 制作一个新的 Word 文档。我对 VSTO 不是很熟悉,所以如果我在这一点上不正确,请原谅我。

不过,我知道您可以使用 Office 互操作库来执行此操作。

要下载这些库,只需搜索“office interop 程序集”,可能包括您想要的 Office 版本(例如:“office interop 程序集 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();

希望有帮助!

于 2010-04-29T18:36:25.880 回答