邮件合并与循环/分组合并
我有一张Excel表,我用来将其合并到Word中。
邮件合并是通过此代码完成的
'starting the mail merge for the main body of the report
Set wdDoc = wdApp.Documents.Open(fNameW)
wdDoc.Activate
wdDoc.MailMerge.OpenDataSource Name:=(fNameE), Revert:=False, Connection:="Entire Spreadsheet", SQLStatement:="SELECT * FROM `'Table of Recommendations$'`", SQLStatement1:=""
With wdDoc.MailMerge
.MainDocumentType = wdFormLetters
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute
For Each wd In ActiveDocument.StoryRanges
With wd.Find
.Text = "(blank)"
.Replacement.Text = ""
.Forward = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
With wd.Find
.Text = "^b"
.Wrap = wdFindContinue
While .Execute
wd.Delete
wd.InsertParagraph
Wend
End With
Next wd
,这是我获得的输出:
现在,我的问题。我想实现的是,推荐编号(b)被插入了第一张表格,仅在推荐编号(a)的基础上,这两个建议来自同一问题 country Comporation 。换句话说,合并过程应循环通过Excel表,如果问题相同,则应将建议组合在一起,省略空白单元格,而不是生成第二个表。您认为这是可能的吗?如果是,您能指出我的方向正确吗?我已经搜索了互联网,但找不到任何解决方案。谢谢。
I have an Excel table, which I use to do a mail merge into word.
The mail merge is done through this code
'starting the mail merge for the main body of the report
Set wdDoc = wdApp.Documents.Open(fNameW)
wdDoc.Activate
wdDoc.MailMerge.OpenDataSource Name:=(fNameE), Revert:=False, Connection:="Entire Spreadsheet", SQLStatement:="SELECT * FROM `'Table of Recommendations
And this is the output I get:
Now, my question. What I would like to achieve is that recommendation number (b) gets inserted in the first table, just under recommendation number (a), based on the fact that the two recommendations arise from the same issue Country Cooperation. In other words, the merge process should loop through the Excel table and if the issue is the same, it should group the recommendations together, omit the blank cells, and not generate the second table. Do you think this is possible? If yes, can you point me in the right direction? I have searched allover the internet but have not been able to find any solution. Thank you.
`", SQLStatement1:=""
With wdDoc.MailMerge
.MainDocumentType = wdFormLetters
.Destination = wdSendToNewDocument
.SuppressBlankLines = True
With .DataSource
.FirstRecord = wdDefaultFirstRecord
.LastRecord = wdDefaultLastRecord
End With
.Execute
For Each wd In ActiveDocument.StoryRanges
With wd.Find
.Text = "(blank)"
.Replacement.Text = ""
.Forward = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
With wd.Find
.Text = "^b"
.Wrap = wdFindContinue
While .Execute
wd.Delete
wd.InsertParagraph
Wend
End With
Next wd
And this is the output I get:
Now, my question. What I would like to achieve is that recommendation number (b) gets inserted in the first table, just under recommendation number (a), based on the fact that the two recommendations arise from the same issue Country Cooperation. In other words, the merge process should loop through the Excel table and if the issue is the same, it should group the recommendations together, omit the blank cells, and not generate the second table. Do you think this is possible? If yes, can you point me in the right direction? I have searched allover the internet but have not been able to find any solution. Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我确实建议您更改数据!包括您现在在两个单元中具有的“推荐文本”值,现在将其纳入一个多行细胞:
I do propose that you change your data! Include e.g. "RecommendationText" values which you have in two cells now into one multiline cell:
将串联列添加到数据库数据的副本
= if($ c13 =“”,e12& char(10)& d13,e12)...对于下面的第12行
中的第12行
过滤您的非空行的数据(例如col“ colding croppppent”)< /p>
。

。
带有“串联列”的屏幕截图
。

。
屏幕截图过滤数据
add a concatenating column to a copy of your database data
=IF($C13="",E12&CHAR(10)&D13,E12) ... for the row 12 in the example below
filter your data for non empty lines (e.g. col "Background")
.

.
Screenshot with "concatenating column"
.

.
Screenshot filtered data
根据@macropod给出的建议,我解决了问题。 Using the guideline available at https:/ /www.msofficeforums.com/mail-merge/38721-microsoft-word-catalogue-directory-mailmerge-tutorial.html ,我能够解决问题。我按照《 AFIRE指南》第4页的第4页设置了邮件合并模板,并添加了“木匠”宏观宏观,以前指南的第20/21页中描述并解决了我的问题。下面我写的代码示例:
I have solved my problem following the suggestions given by @macropod. Using the guideline available at https://www.msofficeforums.com/mail-merge/38721-microsoft-word-catalogue-directory-mailmerge-tutorial.html, I was able to sort the issue out. I set-up the mail merge template as described in page 4 of the afire guideline, added the table joiner macro described in pages 20/21 of the afore guidelines and solved my issue. Below a sample of the code I wrote: