vba 如何将自定义字段提供程序添加到 MS Word?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4917154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:37:38  来源:igfitidea点击:

How to add a custom field provider to MS Word?

vbams-wordfield

提问by Al Kepp

Foreword: I want to allow users to define high quality document templates and then inject there data from our information system and print the result. I think MS Word is a great starting point, because this work is aimed to business letters etc., not data reports.

前言:我想让用户定义高质量的文档模板,然后从我们的信息系统中注入数据并打印结果。我认为 MS Word 是一个很好的起点,因为这项工作的目标是商业信函等,而不是数据报告。

Question: Is it possible to add a custom field provider to MS Word?

问题:是否可以向 MS Word 添加自定义字段提供程序?

I don't have English MS Word, so I must try to describe what I mean in a few sentences. Normally we can insert "fields" like author name, current date etc. These fields work seamlessly. We can switch view of fileds between data and definition. Definion of author field looks like this: { AUTHOR \\* MERGEFORMAT }.

我没有英文 MS Word,所以我必须试着用几句话来描述我的意思。通常我们可以插入“字段”,如作者姓名、当前日期等。这些字段可以无缝工作。我们可以在数据和定义之间切换文件视图。笔者现场的Definion看起来是这样的:{ AUTHOR \\* MERGEFORMAT }

Now I want to inject external data into documents and let user specify where to put them. A user should define a document template and mark spots where external data should be injected. Since Word users generally aren't IT experts, the easiest way for them is to use some macros or "insert field" option to do it. So I want to define my own set of fields and connect Word to my custom field and data provider. How to do it? I am unable to find any documentation on this.

现在我想将外部数据注入文档并让用户指定放置它们的位置。用户应定义文档模板并标记应注入外部数据的位置。由于 Word 用户通常不是 IT 专家,因此对他们来说最简单的方法是使用一些宏或“插入字段”选项来完成。所以我想定义我自己的一组字段并将 Word 连接到我的自定义字段和数据提供程序。怎么做?我找不到任何关于此的文档。

I think this approach is better than using sql database connection or something like that, because I want to let the external source define list of known fields and their values, not the docx document itself. Also, data source won't actually be an sql database.

我认为这种方法比使用 sql 数据库连接或类似方法要好,因为我想让外部源定义已知字段及其值的列表,而不是 docx 文档本身。此外,数据源实际上不会是 sql 数据库。

回答by Todd Main

Absolutely, this is the exact kind of scenario that Content Controls and CustomXMLParts were built for (Word 2007/2010 only, not earlier .doc format).

毫无疑问,这正是 Content Controls 和 CustomXMLParts 所针对的场景类型(仅限 Word 2007/2010,而不是早期的 .doc 格式)。

Most of the Word Developer Center home pagedeals with these two: Content Controls and CustomXMLParts. If you go this route, you'll find the Word Content Control Toolkitan invaluable resource as well, especially when just starting out.

大多数Word 开发人员中心主页都处理这两个:内容控件和 CustomXMLParts。如果您走这条路,您会发现Word 内容控制工具包也是一种宝贵的资源,尤其是在刚开始时。

From an end-user perspective, it could be as simple as just creating buttons on the Ribbonfor insertable Content Controls via a template or document add-in (VSTO or VBA).

从最终用户的角度来看,它可能就像通过模板或文档加载项(VSTO 或 VBA)在功能区上为可插入的内容控件创建按钮一样简单。

回答by Michael Dillon

Yes you can do this by using Custom Document Properties as placeholders and then use some VBA code to set those properties to whatever you want. You can get the data via ODBC or from an Excel spreadsheet or from a text file.

是的,您可以通过使用自定义文档属性作为占位符来完成此操作,然后使用一些 VBA 代码将这些属性设置为您想要的任何内容。您可以通过 ODBC 或从 Excel 电子表格或文本文件获取数据。

First of all, experiment manually by going into File, Propertiesand creating some custom properties. Give them a value and then, in the document, insert some DocPropertyfields. If you can't find DocPropertyin your language version of Word then look through a list of the fields like Authoretc. Since field names are visible to end users they might have been translated.

首先,通过进入File, Properties并创建一些自定义属性来手动进行实验。给它们一个值,然后在文档中插入一些DocProperty字段。如果DocProperty在您的 Word 语言版本中找不到,请查看字段列表,例如Author等。由于字段名称对最终用户可见,因此它们可能已被翻译。

Then in order to complete your document template, create a VBA function that uses SetProperty. Read this article for more details.It is up to you whether the VBA is triggered by opening the file or whether you add a menu item to do that.

然后为了完成您的文档模板,创建一个使用SetProperty. 阅读这篇文章了解更多详情。是否通过打开文件触发 VBA 或是否添加菜单项来执行此操作取决于您。

No need for special controls or any commercial add-ons. I'm going to add a VBA tag to your question since this is really a VBA programming question. In fact, this has been possible since Word for Windows 1.0.

无需特殊控件或任何商业附加组件。我将在您的问题中添加一个 VBA 标签,因为这确实是一个 VBA 编程问题。事实上,从 Word for Windows 1.0 开始,这已经成为可能。

Using SetPropertyin VBA is a bit more complex now. I got the following snippet of code from this forum posting.

使用SetPropertyVBA中是一个比较复杂了。我从这个论坛帖子中得到了以下代码片段

object docProps = wdDoc.CustomDocumentProperties; Type docPropsType = docProps.GetType(); object Prop = docPropsType.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, docProps, new object[] {propName} ); Type PropType = Prop.GetType(); PropType.InvokeMember("Item", BindingFlags.Default | BindingFlags.SetProperty, null, docProps, new object[] {propName,propValue} );

对象 docProps = wdDoc.CustomDocumentProperties; 类型 docPropsType = docProps.GetType(); object Prop = docPropsType.InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, docProps, new object[] {propName} ); 类型 PropType = Prop.GetType(); PropType.InvokeMember("Item", BindingFlags.Default | BindingFlags.SetProperty, null, docProps, new object[] {propName,propValue} );

回答by DarinH

If you want a fairly decent prebuilt solution, check out Windward reports.

如果您想要一个相当不错的预构建解决方案,请查看 Windward 报告。

Yeah, the name makes it sound like a reporting tool, but in reality, it's exactly what you're describing. They have a Word add in that allows users to easily mark up a word doc with fields to be inserted from your data source.

是的,这个名字听起来像一个报告工具,但实际上,它正是你所描述的。他们有一个 Word 插件,允许用户轻松地标记带有要从数据源插入的字段的 Word 文档。

I built a very similar system for a law firm. Windward didn't do quite everything I needed it to do, but at the same time, it's pretty powerful.

我为一家律师事务所建立了一个非常相似的系统。Windward 没有做我需要它做的所有事情,但同时,它非常强大。