locked
Word Mail Merge With Asp .Net RRS feed

  • Question

  • User626389161 posted

    Hi all,

    I need to develop more than 300 covering letter for my project. And it will be time consuming job. So i planned to design covering letter by easiest way. If i use crystal report, that may be risk to design and future alteration. I wish to do this by easiest way. How to use Word Mail Merge from asp .net? My idea is passing input to word mail merge from asp .net form and open that predesigned document with given input related data by clicking button. If any other concept exists,please refer me.

    Friday, April 19, 2013 10:16 AM

All replies

  • User626389161 posted

    Split Into Documents

    _________________________

    private void splitIntoDocs()
            {
                object objMiss = Type.Missing;
                object templateDoc = "D:\\template1.doc"; //Its is a predefined template
                object mergedDoc = "D:\\Merged.doc";
                object objTrue = true;
                object objFalse = false;

                label1.Text = "Creating merged doc file... ";
                System.Windows.Forms.Application.DoEvents();

                Word.Document docWord = new Word.Document();
                //open Microsoft Word
                Word.ApplicationClass mergeWord = new Word.ApplicationClass();
                mergeWord.Visible = false;


                object missing = System.Reflection.Missing.Value;
                object unit = Word.WdUnits.wdStory;
                object direction = Word.WdUnits.wdCharacter;
                object count = 1;
                object fileFormat = Word.WdSaveFormat.wdFormatDocument;
                object embedTrueTypeFonts = true;
                object saveChanges = false;
                object docName;
                string clientNum;
                object format = Word.WdOpenFormat.wdOpenFormatText;

              //When Creating ODC file. you should tick save password. Else every time it will ask password from you by dialog box
                string source = @"C:\Users\Documents\My Data Sources\Test1.odc"; // create a generic select statement for this file.


                using (SqlConnection con = new SqlConnection("Data Source = CCS; Database = Security; User ID = sa; pwd = sa")) // loop thru the database and create each doc with each record retrieved
                {
                    SqlDataReader sdrSelect = null;
                    SqlCommand scSelect = null;
                    scSelect = new SqlCommand("SELECT UserID " +
                                              "FROM Users", con);
                    con.Open();
                    sdrSelect = scSelect.ExecuteReader();
                    while (sdrSelect.Read())
                    {
                        //open Template file
                        docWord = mergeWord.Documents.Open(ref templateDoc, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);
                        docWord.Select();

                        //open datasource
                        docWord.MailMerge.OpenDataSource(source, ref format, ref objFalse, ref objMiss, ref objTrue, ref objFalse, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);

                        docWord.MailMerge.Destination = Word.WdMailMergeDestination.wdSendToNewDocument;
                        try
                        {

                            // [Users] table name should be square bracketed to avoid command error
                            docWord.MailMerge.DataSource.QueryString = "SELECT * FROM [Users] WHERE UserID = '" + sdrSelect[0].ToString() + "'"; // select only one record for each loop ir order for this code to work correctly
                            docWord.MailMerge.DataSource.FirstRecord = (int)Word.WdMailMergeDefaultRecord.wdDefaultFirstRecord;
                            docWord.MailMerge.DataSource.LastRecord = (int)Word.WdMailMergeDefaultRecord.wdDefaultLastRecord;
                            docWord.MailMerge.Execute(ref objFalse);

                            mergeWord.ActiveDocument.SaveAs(ref mergedDoc, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss, ref objMiss);
                            
                            docWord.Close(ref objFalse, ref objMiss, ref objMiss);
                            mergeWord.ActiveDocument.Close(ref saveChanges, ref objMiss, ref objMiss);

                            clientNum = sdrSelect[0].ToString(); // use the value returned to name your child doc.
                            docName = "D:\\Temp\\" + clientNum + ".doc";
                            File.Move(mergedDoc.ToString(), docName.ToString()); // rename the mergeDoc file to your child doc file's name
                        }
                        catch
                        { // if the select didn't return a row then loop again
                            docWord.Close(ref objFalse, ref objMiss, ref objMiss);
                            continue;
                        }
                    } //while
                    if (sdrSelect.HasRows) // only do the stuff below if mail merge went thru
                    {
                        mergeWord.Quit(ref saveChanges, ref objMiss, ref objMiss);
                        Marshal.ReleaseComObject(docWord);
                        mergeWord = null;
                        docWord = null;
                        templateDoc = null;
                        mergedDoc = null;


                        GC.Collect();
                        GC.WaitForPendingFinalizers();
                    }
                    con.Close();
                } //using
            }

    Monday, April 22, 2013 3:29 AM
  • User-245986444 posted

    There is a template based toolkit for .NET named Docentric Toolkit which is a very versatile tool and might suit your need.

    Thursday, April 25, 2013 7:43 AM
  • User-2082479761 posted

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Spire.Doc;

    namespace MailMerge
    {
    class Program
    {
    static void Main(string[] args)
    {
    Document doc = new Document();
    doc.LoadFromFile(@"../../Microsoft Office.doc",FileFormat.Doc);
    string[] Datevalues = { string.Format("{0:d}", System.DateTime.Now), string.Format("{0:d}", System.DateTime.Now), string.Format("{0:d}", System.DateTime.Now), string.Format("{0:d}", System.DateTime.Now), string.Format("{0:d}", System.DateTime.Now) };
    string[] FieldName = { "SubGrantPAStartDateValue", "SubGrantPAEndDateValue", "SubGrantPAExtensionDateValue", "SubGrantPSStartDateValue", "SubGrantPSEndDateValue" };
    doc.MailMerge.Execute(FieldName, Datevalues);
    doc.SaveToFile(@"../../Microsoft OfficeMergeResult.doc",FileFormat.Doc);
    doc.Close();
    System.Diagnostics.Process.Start(@"../../Microsoft OfficeMergeResult.doc");
    }
    }
    }

    replace "Microsoft OfficeMergeResult" to your file

    hope it could help you

    Wednesday, August 21, 2013 5:12 AM
  • User-2082479761 posted

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Spire.Doc;

    namespace MailMerge
    {
    class Program
    {
    static void Main(string[] args)
    {
    Document doc = new Document();
    doc.LoadFromFile(@"../../Microsoft Office.doc",FileFormat.Doc);
    string[] Datevalues = { string.Format("{0:d}", System.DateTime.Now), string.Format("{0:d}", System.DateTime.Now), string.Format("{0:d}", System.DateTime.Now), string.Format("{0:d}", System.DateTime.Now), string.Format("{0:d}", System.DateTime.Now) };
    string[] FieldName = { "SubGrantPAStartDateValue", "SubGrantPAEndDateValue", "SubGrantPAExtensionDateValue", "SubGrantPSStartDateValue", "SubGrantPSEndDateValue" };
    doc.MailMerge.Execute(FieldName, Datevalues);
    doc.SaveToFile(@"../../Microsoft OfficeMergeResult.doc",FileFormat.Doc);
    doc.Close();
    System.Diagnostics.Process.Start(@"../../Microsoft OfficeMergeResult.doc");
    }
    }
    }

    replace "Microsoft OfficeMergeResult" to your file

    hope it could help you

    Wednesday, August 21, 2013 5:12 AM