Merge letters (Excel to Word) using C #

I was wondering if anyone knows how to perform a mail merge using an Excel file as a data source in order to populate fields in a Word template? I want to use interop for Word if I could ... but I have some difficulty finding the code to do this. Does anyone have a syntax for this? Thank you in advance.

+1


a source to share


3 answers


A very useful method of teaching automatic actions in MS Word is to actually perform the action manually with the "Record Macro" enabled.

Once you have a VBA macro, it is easy enough to convert it to VB.NET or C # where interop is used. I usually set up VBA manually manually in Word, so I can test this first before switching to .NET using the interop layer.

I don't know much about mailmerge, but these are some of the VBA generated while I recorded the 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 complete code here, but hopefully this will give you some ideas.

+3


a source


Word (at least in the 2007 version, which is what I'm looking for) can do this out of the box. Just choose an Excel file as your data source. What are you trying to do in your code?



0


a source


I am using this Third Party Control. You can read your xls file into a data table and then tell the control to merge it.

0


a source







All Articles