none
Word MailMerge automation to generate a new Table.

    Question

  • Hello,

    I need to create a Document using Mail Merge  in which User has created a table,

    and I need to duplicate the rows in it using MailMerge.

    How to Generate Rows in a single table using Mail Merge C# ?


    I am able to generate new table in temporary data source file,

    but it creates a new table under the original table.
    Saturday, September 08, 2007 1:11 PM

All replies

  • Hi Shah,

     

    Based on my understanding, you are using MailMerge.CreateDataSource() to create a data source file. In your data source file, you want to duplicate some rows from the existed table into the same table, right? If I have any misunderstanding of your scenario, please feel free to point it out.

    After you create the data source file, Word will help us to insert a table into the data file. We can get the table’s handle, and use Table.Rows.Add() method to add a row. Then copy the data you want to duplicate into the newly added row.

    There is a very specific KB article to show a sample of MailMerge in the following link:

    http://support.microsoft.com/kb/301659

    Hope it can help you get a start.

     

     

    Thanks

    Ji

     

    Wednesday, September 12, 2007 5:37 AM
  • Hi Ji,

     

     

    That all things I know,

    I mistake didn't write in detail, let me describe my problem.

     

    I am using Temporary Table for Mail Merge as link you provided already of MSDN (http://support.microsoft.com/kb/301659)

     

    My Problem is I need to create that table in such a way that, when Mail Merge is performed it creates multiple entries in the New Document per page.

     

    using that temporary table we can create table that will replace our existing table entries with DATA mail merged.

    But I want that when a we have put the mail merge field in TABLE we have to duplicate it in the same table as rows.

     

    e.g. (example)

     

    ----------------------------------------------BEFORE MAIL MERGE--------------------------------------------

    --------------------------------------------------------------------------------------------------------------------------

    // below is the table created in the main document which is about to be merged using createdatasource.

    ------------------------------------

              <<party_name>>

    ------------------------------------

    <<party_name_address>>

    -----------------------------------

    <<party_name_address>>

    -------------------------------------

     

    ----------------------------------------------AFTER MAIL MERGE--------------------------------------------

    --------------------------------------------------------------------------------------------------------------------------

     

    ------------------------------------

              Party Name 1

    ------------------------------------

    Address 1

    -----------------------------------

    Address 2

    -----------------------------------

    --------------------------SECTION BREAK--------------------

    ------------------------------------

              Party Name 2

    ------------------------------------

    Address Party Name3

    -----------------------------------

    Address Party Name4

    ----------------------------------

     

     

    SO I think You Understand the problem.

     

     

    Problem is that How to create the multiple rows in the new document which is merged.

     

    Regards,

    Japan Shah

    Wednesday, September 12, 2007 6:12 AM
  • Hi,

     

    I am still not very sure about your objective exactly. But from your words, it seems that you want insert a table into the before-merged document. And you also want to insert two duplicate MergeFields in the table. So after the MailMerge is finished, all MergeFields in every sections will be put in tables. And there are two same rows in each table, right?

    But when you try to insert a table into the before-merged document, the original table entry is covered by our newly added table, so it will through exception when running the codes, right? To walk around this, we can keep a variable at Class level to refer the original table.

    And if you want to insert another duplicate email address field, just add it twice like these:

    Code Snippet

        wrdMergeFields.Add(myTable.Cell(2, 1).Range, "Address");

                wrdMergeFields.Add(myTable.Cell(3, 1).Range, "Address");

     

    I post all the modified codes as follows, and you can run it to see if this is what you want. Hope it will help!

    Code Snippet

    Word.Application wrdApp;

            Word._Document wrdDoc;

            Object oMissing = System.Reflection.Missing.Value;

            Object oFalse = false;

            Word.Table wrdTable;

     

            private void InsertLines(int LineNum)

            {

                int iCount;

     

                // Insert "LineNum" blank lines.   

                for (iCount = 1; iCount <= LineNum; iCount++)

                {

                    wrdApp.Selection.TypeParagraph();

                }

            }

     

            private void FillRow(Word._Document oDoc, int Row, string Text1,

                string Text2, string Text3, string Text4,bool flag)

            {

                // Insert the data into the specific cell.

                if (flag)

                {

                    oDoc.Tables[1].Cell(Row, 1).Range.InsertAfter(Text1);

                    oDoc.Tables[1].Cell(Row, 2).Range.InsertAfter(Text2);

                    oDoc.Tables[1].Cell(Row, 3).Range.InsertAfter(Text3);

                    oDoc.Tables[1].Cell(Row, 4).Range.InsertAfter(Text4);

                }

                else

                {

                    wrdTable.Cell(Row, 1).Range.InsertAfter(Text1);

                    wrdTable.Cell(Row, 2).Range.InsertAfter(Text2);

                    wrdTable.Cell(Row, 3).Range.InsertAfter(Text3);

                    wrdTable.Cell(Row, 4).Range.InsertAfter(Text4);

                }

            }

     

            private void CreateMailMergeDataFile()

            {

                Word._Document oDataDoc;

                int iCount;

     

                Object oName = "C:\\DataDoc.doc";

                Object oHeader = "FirstName, LastName, Address, CityStateZip";

                wrdDoc.MailMerge.CreateDataSource(ref oName, ref oMissing,

                    ref oMissing, ref oHeader, ref oMissing, ref oMissing,

                    ref oMissing, ref oMissing, ref oMissing);

     

                // Open the file to insert data.

                oDataDoc = wrdApp.Documents.Open(ref oName, ref oMissing,

                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,

                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,

                        ref oMissing, ref oMissing, ref oMissing, ref oMissing,

                        ref oMissing, ref oMissing );

     

                for (iCount = 1; iCount <= 2; iCount++)

                {

                    oDataDoc.Tables[1].Rows.Add(ref oMissing);

                }

                // Fill in the data.

                FillRow(oDataDoc, 2, "Steve", "DeBroux",

                    "4567 Main Street", "Buffalo, NY  98052",true);

                FillRow(oDataDoc, 3, "Jan", "Miksovsky",

                    "1234 5th Street", "Charlotte, NC  98765",true);

                FillRow(oDataDoc, 4, "Brian", "Valentine",

                    "12348 78th Street  Apt. 214",

                    "Lubbock, TX  25874",true);

                // Save and close the file.

                oDataDoc.Save();

                oDataDoc.Close(ref oFalse, ref oMissing, ref oMissing);

            }

     

    Thanks

    Ji

     

    Wednesday, September 12, 2007 8:58 AM
  • Code Snippet

           private void button1_Click(object sender, System.EventArgs e)

            {

                Word.Selection wrdSelection;

                Word.MailMerge wrdMailMerge;

                Word.MailMergeFields wrdMergeFields;

                object unit = Word.WdUnits.wdCharacter;

                object count = 1;

                object extend = Word.WdMovementType.wdMove;

               

                string StrToAdd;

     

                // Create an instance of Word  and make it visible.

                wrdApp = new Word.Application();

                wrdApp.Visible = true;

     

                // Add a new document.

                wrdDoc = wrdApp.Documents.Add(ref oMissing, ref oMissing,

                    ref oMissing, ref oMissing);

                wrdDoc.Select();

     

                wrdSelection = wrdApp.Selection;

                wrdMailMerge = wrdDoc.MailMerge;

     

                // Create a MailMerge Data file.

                CreateMailMergeDataFile();

     

                // Create a string and insert it into the document.

                StrToAdd = "State University\r\nElectrical Engineering Department";

                wrdSelection.ParagraphFormat.Alignment =

                    Word.WdParagraphAlignment.wdAlignParagraphCenter;

                wrdSelection.TypeText(StrToAdd);

     

                InsertLines(4);

     

                // Insert merge data.

                wrdSelection.ParagraphFormat.Alignment =

                    Word.WdParagraphAlignment.wdAlignParagraphLeft;

     

                Word.Table myTable = wrdDoc.Tables.Add(wrdSelection.Range, 4, 1, ref oMissing, ref oMissing);

                myTable.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal].LineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;

                myTable.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderHorizontal].LineWidth = Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth075pt;

                myTable.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].LineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;

                myTable.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderLeft].LineWidth = Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth075pt;

                myTable.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight].LineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;

                myTable.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderRight].LineWidth = Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth075pt;

                myTable.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop].LineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;

                myTable.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderTop].LineWidth = Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth075pt;

                myTable.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleSingle;

                myTable.Borders[Microsoft.Office.Interop.Word.WdBorderType.wdBorderBottom].LineWidth = Microsoft.Office.Interop.Word.WdLineWidth.wdLineWidth075pt;

     

                wrdMergeFields = wrdMailMerge.Fields;

                myTable.Cell(1, 1).Range.Select();

                wrdMergeFields.Add(wrdSelection.Range, "FirstName");

                wrdSelection.TypeText(" ");

                wrdMergeFields.Add(wrdSelection.Range, "LastName");

               

                wrdMergeFields.Add(myTable.Cell(2, 1).Range, "Address");

                wrdMergeFields.Add(myTable.Cell(3, 1).Range, "Address");

                wrdMergeFields.Add(myTable.Cell(4, 1).Range, "CityStateZip");

     

                myTable.Range.Select();

                count = 100;

                wrdSelection.MoveRight(ref unit,ref count,ref extend);

                InsertLines(2);

     

                // Right justify the line and insert a date field

                // with the current date.

                wrdSelection.ParagraphFormat.Alignment =

                    Word.WdParagraphAlignment.wdAlignParagraphRight;

     

                Object objDate = "dddd, MMMM dd, yyyy";

                wrdSelection.InsertDateTime(ref objDate, ref oFalse, ref oMissing,

                    ref oMissing, ref oMissing);

     

               

     

     

    Wednesday, September 12, 2007 9:07 AM
  • Code Snippet

              InsertLines(2);

     

                // Justify the rest of the document.

                wrdSelection.ParagraphFormat.Alignment =

                    Word.WdParagraphAlignment.wdAlignParagraphJustify;

     

                wrdSelection.TypeText("Dear ");

                wrdMergeFields.Add(wrdSelection.Range, "FirstName");

                wrdSelection.TypeText(",");

                InsertLines(2);

     

                // Create a string and insert it into the document.

                StrToAdd = "Thank you for your recent request for next " +

                    "semester's class schedule for the Electrical " +

                    "Engineering Department. Enclosed with this " +

                    "letter is a booklet containing all the classes " +

                    "offered next semester at State University.  " +

                    "Several new classes will be offered in the " +

                    "Electrical Engineering Department next semester.  " +

                    "These classes are listed below.";

                wrdSelection.TypeText(StrToAdd);

     

                InsertLines(2);

     

                // Insert a new table with 9 rows and 4 columns.

                wrdTable = wrdDoc.Tables.Add(wrdSelection.Range, 9, 4,

                    ref oMissing, ref oMissing);

                // Set the column widths.

                wrdTable.Columns[1].SetWidth(51, Word.WdRulerStyle.wdAdjustNone);

                wrdTable.Columns[2].SetWidth(170, Word.WdRulerStyle.wdAdjustNone);

                wrdTable.Columns[3].SetWidth(100, Word.WdRulerStyle.wdAdjustNone);

                wrdTable.Columns[4].SetWidth(111, Word.WdRulerStyle.wdAdjustNone);

                // Set the shading on the first row to light gray.

                wrdTable.Rows[1].Cells.Shading.BackgroundPatternColorIndex =

                    Word.WdColorIndex.wdGray25;

                // Bold the first row.

                wrdTable.Rows[1].Range.Bold = 1;

                // Center the text in Cell (1,1).

                wrdTable.Cell(1, 1).Range.Paragraphs.Alignment =

                    Word.WdParagraphAlignment.wdAlignParagraphCenter;

     

                // Fill each row of the table with data.

                FillRow(wrdDoc, 1, "Class Number", "Class Name",

                    "Class Time", "Instructor",false);

                FillRow(wrdDoc, 2, "EE220", "Introduction to Electronics II",

                    "1:00-2:00 M,W,F", "Dr. Jensen",false);

                FillRow(wrdDoc, 3, "EE230", "Electromagnetic Field Theory I",

                    "10:00-11:30 T,T", "Dr. Crump",false);

                FillRow(wrdDoc, 4, "EE300", "Feedback Control Systems",

                    "9:00-10:00 M,W,F", "Dr. Murdy",false);

                FillRow(wrdDoc, 5, "EE325", "Advanced Digital Design",

                    "9:00-10:30 T,T", "Dr. Alley",false);

                FillRow(wrdDoc, 6, "EE350", "Advanced Communication Systems",

                    "9:00-10:30 T,T", "Dr. Taylor",false);

                FillRow(wrdDoc, 7, "EE400", "Advanced Microwave Theory",

                    "1:00-2:30 T,T", "Dr. Lee",false);

                FillRow(wrdDoc, 8, "EE450", "Plasma Theory",

                    "1:00-2:00 M,W,F", "Dr. Davis",false);

                FillRow(wrdDoc, 9, "EE500", "Principles of VLSI Design",

                    "3:00-4:00 M,W,F", "Dr. Ellison",false);

     

                // Go to the end of the document.

                Object oConst1 = Word.WdGoToItem.wdGoToLine;

                Object oConst2 = Word.WdGoToDirection.wdGoToLast;

                wrdApp.Selection.GoTo(ref oConst1, ref oConst2, ref oMissing, ref oMissing);

                InsertLines(2);

     

                // Create a string and insert it into the document.

                StrToAdd = "For additional information regarding the " +

                    "Department of Electrical Engineering, " +

                    "you can visit our Web site at ";

                wrdSelection.TypeText(StrToAdd);

                // Insert a hyperlink to the Web page.

                Object oAddress = "http://www.ee.stateu.tld";

                Object oRange = wrdSelection.Range;

                wrdSelection.Hyperlinks.Add(oRange, ref oAddress, ref oMissing,

                    ref oMissing, ref oMissing, ref oMissing);

                // Create a string and insert it into the document

                StrToAdd = ".  Thank you for your interest in the classes " +

                    "offered in the Department of Electrical " +

                    "Engineering.  If you have any other questions, " +

                    "please feel free to give us a call at " +

                    "555-1212.\r\n\r\n" +

                    "Sincerely,\r\n\r\n" +

                    "Kathryn M. Hinsch\r\n" +

                    "Department of Electrical Engineering \r\n";

                wrdSelection.TypeText(StrToAdd);

     

                // Perform mail merge.

                wrdMailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;

                wrdMailMerge.Execute(ref oFalse);

     

                // Close the original form document.

                wrdDoc.Saved = true;

                wrdDoc.Close(ref oFalse, ref oMissing, ref oMissing);

     

     

                // Release References.

                wrdSelection = null;

                wrdMailMerge = null;

                wrdMergeFields = null;

                wrdDoc = null;

                wrdApp = null;

     

                // Clean up temp file.

                System.IO.File.Delete("C:\\DataDoc.doc");

            }

     

     

     

    Wednesday, September 12, 2007 9:08 AM


  • My Problem is very simple.

    I just need to create a Table in which multiple rows are created per table per page.

    what the MSDN Example can do is it can create 1 row per page when merged.

    I want Temporary table contains 1 row which can generates 5 rows in parent table's Cell.

    we can call one2many entries in final new Document.
    Wednesday, September 12, 2007 9:15 AM