简体   繁体   English

邮件在C#中合并n个副本

[英]Mail merge in C# with n copies

I just want to create a C# program which will read a word template and create n number of copies of it with mail merge feature.The data to replace is Name and Address the rest of the things in the template should remains the same. 我只想创建一个C#程序,它将读取一个单词模板并使用邮件合并功能创建n个副本。要替换的数据是名称和地址,模板中的其余内容应该保持不变。 Can any one tell me how to do this ? 谁能告诉我该怎么做?

You can use Aspose.Word for handling the Word Object model without having to have office installed (to use interop) where the program is supposed to run, i'm using Aspose.Word to generate word documents. 您可以使用Aspose.Word处理Word Object模型,而不必在应该运行程序的位置安装Office(以使用interop),我正在使用Aspose.Word生成Word文档。

Link to Aspose: http://www.aspose.com/categories/file-format-components/aspose.words-for-.net-and-java/default.aspx 链接到Aspose: http : //www.aspose.com/categories/file-format-components/aspose.words-for-.net-and-java/default.aspx

And it works quite decent :) 它的工作相当不错:)

I did this in Java - (broken link) working example here with source code. 我在Java中执行了此操作-(断开的链接) 此处带有源代码的示例

Here's the idea: use MS-Word to design and construct the document you want to send. 这就是想法:使用MS-Word设计和构建您要发送的文档。 Save it as XML (either Word-ML or the new .docx format). 将其另存为XML(Word-ML或新的.docx格式)。 Then, using a text editor, replace the fields in the document with placeholder tags, like @@NAME where the name should go, and @@ADDRESS for the Address, etc. The tag names don't matter. 然后,使用文本编辑器,将文档中的字段替换为占位符标签,例如名称应使用的@@ NAME和地址的@@ ADDRESS等。标签名称无关紧要。

Then, build a replacement logic - either using XSLT or even a simple string-based replace function, and iteratively replace the tags with actual data values. 然后,建立替换逻辑-使用XSLT甚至是简单的基于字符串的替换函数,然后用实际数据值迭代替换标签。 Save each modified doc. 保存每个修改的文档。

Easy peasy. 十分简单。

You could use the same design in C# - actually it would be EASIER. 您可以在C#中使用相同的设计-实际上,它更容易。

I am not sure whether you wish to run a mailmerge or to copy a template. 我不确定您是要运行邮件合并还是要复制模板。 I cannot help you with c#, but this snippet of VBA might give you some ideas. 我不能用c#帮助您,但是VBA的这一片段可能会给您一些想法。

 strDir = CurrentProject.Path  

 strMailmergeDataFilename = strDir & Format(Now, "yymmdd_hhnnss") & ".txt"

' Create CSV from database for use with mailmerge '
' This is a separate function that simply exports the sql '
' ExportSQLToCSV SQL, strMailmergeDataFilename '

'Open merge template '
Set objWordDoc = GetObject(strDir & MergeDocumentFilename, "Word.Document")

objWordDoc.Application.Visible = True      

'Format:=0 ''0 = wdOpenFormatAuto'
'Add the data source '
objWordDoc.MailMerge.OpenDataSource _
    Name:=strMailmergeDataFilename, ConfirmConversions:=False, _
    ReadOnly:=False, LinkToSource:=True, AddToRecentFiles:=False, _
    PasswordDocument:="", PasswordTemplate:="", WritePasswordDocument:="", _
    WritePasswordTemplate:="", Revert:=False, Format:=0, _
    Connection:="", SQLStatement:="", SQLStatement1:=""

'Type some text at a bookmark, you could use .range property ' 
Selection.Goto What:=wdGoToBookmark, Name:="signaturetext"
Selection.TypeText Text:="You are here"

'Run mailmerge '
objWordDoc.MailMerge.Destination = 0 '0 = wdSendToNewDocument'

objWordDoc.MailMerge.Execute

objWordDoc.Application.ActiveDocument.PrintPreview

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM