邮件合并使用C#(Excel中字) [英] Mail Merge (Excel to Word) using C#
问题描述
我想知道是否有人知道如何使用Excel文件作为数据源,以填补在Word模板字段进行邮件合并?我想如果我可以使用互操作的字......但我有这个发现代码中的一些困难。
没有任何人有这方面的任何语法?谢谢你在前进。
I was wondering if anybody knew how to do a mail merge using an Excel file as a datasource, to fill in fields on a Word template? I wish to use the interop for Word if i could...but am having a few difficulties finding code for this. Does anybody have any syntax for this? Thank you in advance.
推荐答案
学习如何具体行动在MS Word是实际执行的操作自动化一个非常有用的方法手动录制宏功能。
A very useful method for learning how to automate specific actions in MS Word is to actually perform the action manually with 'Record Macro' enabled.
一旦你的VBA宏它很容易将其转换为VB.NET或使用互操作的C#。我倾向于手动在Word捏捏VBA第一,所以我就可以先使用互操作层转换为.NET语言之前进行测试。
Once you have the VBA macro its easy enough to convert this to VB.NET or C# that uses interop. I tend to tweak the VBA first manually in Word so I can then test this first before converting to a .NET language using the interop layer.
我不知道多少有关邮件合并,但这是一些VBA的产生,而我录制的宏:
I don't know much about mailmerge, but this is some of the VBA generated whilst I recorded a macro:
ActiveDocument.MailMerge.MainDocumentType = wdFormLetters
ActiveDocument.MailMerge.OpenDataSource Name:= _
"c:\Arrays.xlsx", ConfirmConversions:=False, _
ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
WritePasswordTemplate:="", Revert:=False, Format:=wdOpenFormatAuto, _
Connection:= _
"Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source=c:\Arrays.xlsx;Mode=Read;Extended Properties=""HDR=YES;IMEX=1;"";Jet OLEDB:System database="""";Jet OLEDB:Registry Path="""";Jet OLEDB:Engine Type=37;Jet OLEDB:Database Loc" _
, SQLStatement:="SELECT * FROM `Sheet1$`", SQLStatement1:="", SubType:= _
wdMergeSubTypeAccess
我没有包含完整的代码在这里,但希望这会给你一些想法。
I haven't included the full code here, but hopefully this give you some ideas.
这篇关于邮件合并使用C#(Excel中字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!