使用 VSTO 创建新的 Word 文档

发布于 2024-07-05 20:01:29 字数 61 浏览 8 评论 0原文

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

How can I create a new Word document pro grammatically using Visual Studio Tools for Office?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我家小可爱 2024-07-12 20:01:29

您实际上追求的是使用 PIA(主互操作程序集)的办公自动化。

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

请查看 http://support.microsoft.com/kb/316384 以开始使用。 并谷歌“word interop 创建文档”

What you are actually after is Office Automation using the PIA's (Primary Interop Assemblies).

VSTO is actually a set of Managed .net extensions which make writing add-ins for Office far easier. For external interaction VSTO is not used at all (though you can still reference VSTO libraries and use some of the helpers if you wanted to).

Have a look at http://support.microsoft.com/kb/316384 to get you started. And google 'word interop create document'

一口甜 2024-07-12 20:01:29

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

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

其中 objTemplate 可以是文档的模板

请参阅 文档.添加方法

For a VSTO app-level add-in, you can do something like this:

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

where objTemplate can be a template of document

See Documents.Add Method

娇纵 2024-07-12 20:01:29

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

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

要下载这些库,只需搜索“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();

希望有帮助!

Now, I might be wrong on this, but I don't believe you can actually make a new Word doc using VSTO. I'm not intimately familiar with VSTO, so forgive me if I'm incorrect on that point.

I do know that you can use Office Interop libraries to do this, however.

To download the libraries, just do a search for "office interop assemblies," possibly including the Office version you want (ex: "office interop assemblies 2007").

Once you've included the Word Interop assembly into your application (using Add Reference), you can do something like:

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();

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文