Merging mail in C # with n copies

I just want to create a C # program that will read a word pattern and create n number of copies of it with a merge function. The replacement data is the name and the address, the rest of the elements in the template should remain the same, Can anyone tell me how to do this?


source to share

3 answers

You can use Aspose.Word to handle the Word object model without having to install office (to use interop) where the program should run, I use Aspose.Word to create word documents.

Aspose link:

And it works pretty well :)



I did it in Java - (broken link) working example here with source code.

Here's the idea: use MS-Word to design and create the document you want to send. Save it as XML (either Word-ML or the new .docx format). Then, using a text editor, replace the fields in the document with placeholder tags, such as @@ NAME for the name, and @@ ADDRESS for the address, etc. Tag names don't matter.

Then create replacement logic - either with XSLT or even a simple string based replacement function, and iteratively replace the tags with the actual data values. Save each modified document.

Easy peasy.

You can use the same design in C # - it would actually be EASIER.



I'm not sure if you want to launch the mailbox or copy the template. I can't help you with C #, but this VBA snippet might give you some ideas.

 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'






All Articles