如何从 WPF 自动化 Microsoft Word 2003?

发布于 2024-08-17 03:37:06 字数 171 浏览 1 评论 0原文

我有一个 WPF 窗口(使用 C# 作为代码隐藏),其中有一些文本字段。
我想要的是,当用户按下打印按钮时,我想获取这些字段上的信息并使用 Microsoft Word 2003 模板。该模板有一些空白字段,需要使用来自 WPF 窗口的这些信息进行填充。
我如何自动执行此操作?

I have a WPF window( using c# as code behind) that has some text fields.
What I want is, when the user presses the print button I want to take the information on these fields and use Microsoft word 2003 template. The template has some blank fields to be filled with these info coming from WPF widow.
How would I automate word to do this?

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

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

发布评论

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

评论(2

如何视而不见 2024-08-24 03:37:06

这很简单:

  1. 添加对“Microsoft Word 11.0 对象库”的 COM 引用(或使用 Microsoft.Office.Interop.Word 程序集)。您可能需要安装 Visual Studio Tools for Office System 和/或浏览到主互操作程序集,具体取决于您的 VS.NET 和 Office 版本以及您安装的其他内容。

  2. 创建 Word.Application 应用程序对象 var app = new Word.Application()

  3. 使用 var doc = app.Documents.Open(...) 打开文档。请注意,在 C# 3.5 或更低版本中,您必须传递所有参数。对于大多数情况,您可以使用初始化为 System.Reflection.Missing.Value 的变量。

  4. 使用 foreach 迭代 doc.Fields:读取并解析字段的 .Code 范围,然后根据文本框内容更新字段的 .Result 范围。

例如:

foreach(Field f in doc.Fields)
  if(f.Code.Text.Contains("lastName"))
    f.Result.Text = this.LastName;
  ...

这假设您的数据上下文类具有从 XAML 绑定的 DependencyProperty“LastName”,如下所示:

<TextBox Text="{Binding LastName}" />

This is easy:

  1. Add a COM reference to the the "Microsoft Word 11.0 Object Library" (or use the Microsoft.Office.Interop.Word assembly). You may have to in install Visual Studio Tools for Office System and/or browse to your the Primary Interop Assembly, depending on your VS.NET and Office versions and what else you have installed.

  2. Create a Word.Application application object var app = new Word.Application()

  3. Open the document with var doc = app.Documents.Open(...). Note that in C# 3.5 or below you must pass all parameters. You can use a variable initialized to System.Reflection.Missing.Value for most of them.

  4. Iterate through doc.Fields using foreach: Read and parse the field's .Code range, then update the field's .Result range based on the text box content.

For example:

foreach(Field f in doc.Fields)
  if(f.Code.Text.Contains("lastName"))
    f.Result.Text = this.LastName;
  ...

This assumes your data context class has a DependencyProperty "LastName" that is bound from the XAML like this:

<TextBox Text="{Binding LastName}" />
秋日私语 2024-08-24 03:37:06

您从 WPF 窗口执行此操作并不重要。后面的代码应该完成所有的自动化。以下是一些可以为您提供指导或示例的资源:

请注意,不建议在服务器上执行此操作。我知道您的要求是 Wpf,但这可能最终会涉及 Silverlight 项目。

顺便说一句:使用 COM 对象比普通 .NET 对象有点棘手,Office COM 对象更是如此:

< strong>使用 C# 实现 Word 自动化

注意他的初始声明:

Object oMissing = System.Reflection.Missing.Value()
Object oTrue = true;
Object oFalse = false;

这是因为所有方法参数都是“ref”参数,因此不能传递通常的常量,nulltruefalse.

使用托管代码(Visual Basic 或 Visual C# )

自动化示例的完整列表。

That you are doing this from a WPF window is immaterial. The code behind should do all the automation. Below are some resources that may help you with guidance or examples:

Please note that it is not advisable to do this on a server. I know your requirement is for Wpf, but that may end up getting involved in a Silverlight project.

BTW: Using the COM objects is a little trickier than normal .NET objects, and the Office COM objects even more so:

Word Automation using C#

Note his initial declaration:

Object oMissing = System.Reflection.Missing.Value()
Object oTrue = true;
Object oFalse = false;

This is because all method parameters are 'ref' parameters, so you can't pass the usual constants, null, true, and false.

Automation Samples Using Managed Code (Visual Basic or Visual C#)

A comprehensive list of automation samples.

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