仅使用分隔的字段名称的原始MailMerge [英] Primitive MailMerge using just delimited field names

查看:123
本文介绍了仅使用分隔的字段名称的原始MailMerge的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

显然,对于我们的应用,基于模板生成Word文档的正确方法是鼓励管理员用户(他们将照管模板)在模板文档中嵌入正确的合并字段或书签.

Obviously the correct way for our app to generate a Word document based on a template is to encourage the admin users (who will look after the templates) to embed the correct merge fields or bookmarks in the template document.

但是,在过去,我们发现通常不使用MailMerge的典型管理员用户或必须使用合并字段而大大推迟了Word中的任何其他高级"功能.我们已经尝试过为他们做这些事情,提供文档,大量屏幕截图等.但是对他们来说,这一切都是轻率的".

However in the past we have found our typical admin user who ordinarily doesn't use MailMerge or any of the other "advanced" features in Word is significantly put off by having to use merge fields. We have tried doing it for them, producing documentation, lots of screenshots etc. But it's all "fiddly" for them.

它们有几十个模板(所有字母实际上都是不同的简单字母),并且希望对其进行合理的修改.

They have dozens of templates (all just different kinds of really simple letters), and will want to modify them reasonably frequently.

他们真正想要的是能够使用简单的分隔符(例如花括号)标记字段,该分隔符有效地将自制的合并字段标记为我们的应用程序(尽管Word忽略了其含义),如下所示:

What they would really like is to be able to just mark fields with a simple delimiter like a curly brace, which effectively marks a homemade merge field to our app (though Word is oblivious to its significance) as in:

尊敬的{CustomerSurname}

Dear {CustomerSurname}

然后,我们可以使用几行代码来拾取字段,如下所示:

Then we can just pick up the field(s) with several lines of code as in:

w = New Word.Application
d = w.Documents.Open(...)
Dim mergedContent As String = d.Content.Text
mergedContent = mergedContent.Replace("{CustomerSurname}", Customer.Surname)
mergedContent = mergedContent.Replace("{CustomerPostcode}", Customer.Postcode)
d.Content.Text = mergedContent

这感觉很粗糙,但是(对于最终用户)非常简单.

This feels crude, but beautifully simple (for the end user).

还有其他人走这条路线吗?有什么问题吗?我们建议他们不要在文档的常规文本中的其他位置使用"{"和}"字符,但这并不是一个很大的限制.

Has anyone else gone down this route? Anything wrong with it? We would advise them not to use the "{" and "}" character elsewhere in the normal text of the document, but that's not really a significant limitation.

速度?将合并字段跨两行包装吗?还有其他问题吗?

Speed? Wrapping the merge field across two lines? Other problems?

推荐答案

这是我以前查找和替换的代码的一部分.我先尝试了您的代码,但这没有用.这基于Word记录宏时生成的VBA代码.

This is a part of my code that I used to find and replace. I tried your code first but that didn't work. This is based upon the VBA code that Word generates when you record a macro.

range = document.Range()

With range.Find
 .Text = "{" & name & "}"
 .Replacement.Text = NewValueHere
 .Forward = True
 .Wrap = Word.WdFindWrap.wdFindContinue
 .Format = False
 .MatchCase = True
 .MatchWholeWord = False
 .MatchWildcards = False
 .MatchSoundsLike = False
 .MatchAllWordForms = False
End With
range.Find.Execute(Replace:=Word.WdReplace.wdReplaceAll)

我使用此代码是因为文档需要与Office二进制文件格式兼容.如果可以使用Office 2007格式,则无需安装Word.只需解压缩文档,找到word \ document.xml文件,执行String.Replace("[OldValue]", "New value"),保存文件并将其压缩回一个包(docx文件)即可. 我在这里显示的代码非常慢,因为我正在自动执行Word.在我的电脑上,打开Word,编辑具有6个字段的2个文档并关闭应用程序需要花费4秒钟.

I used this code because the documents needed to be compatible with the Office binary file formats. If you can use the Office 2007 format you don't need to have Word installed. Just unzip the document, find the word\document.xml file, do a String.Replace("[OldValue]", "New value"), save the file and zip it back to one package (docx file). The code I displayed here is pretty slow because I'm automating Word. Opening Word, editing 2 documents with 6 fields and closing the app takes 4 seconds on my pc.

这篇关于仅使用分隔的字段名称的原始MailMerge的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆