Specs

Specs

  • NA
  • 2
  • 0

ANOTHER Automated Mail Merge Thread: Help!

May 3 2008 8:32 AM
I have read all the mail merge threads and articles I could find, and I'm a little frustrated at the moment... I really hope someone could help me here!

I found a very simple approach to merging, which I've implemented, and the code is show below:

        //OBJECT OF MISSING "NULL VALUE"
            Object oMissing = System.Reflection.Missing.Value;

            //OBJECTS OF FALSE AND TRUE
            Object oTrue = true;
            Object oFalse = false;
            DataRow row;

            //CREATING OBJECTS OF WORD AND DOCUMENT
            Word.Application oWord = new Word.Application();
            Word.Document oWordDoc = new Word.Document();

            //DEFINE FILE
            openFileDialog.ShowDialog();
            openFileDialog.Filter = "Word Template File (*.dot)|*.dot|Word File (*.doc)|*.doc|All files (*.*)|*.*";

            //SETTING THE VISIBILITY TO TRUE
            oWord.Visible = true;

            //THE LOCATION OF THE TEMPLATE FILE ON THE MACHINE
            Object oTemplatePath = openFileDialog.FileName;

            //ADDING A NEW DOCUMENT FROM A TEMPLATE
            oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);

            bs_results.MoveFirst();
            row = ((DataRowView)bs_results.Current).Row;

            foreach (Word.Field myMergeField in oWordDoc.Fields)
            {
                String fieldtext = myMergeField.Code.Text;

                if (fieldtext.StartsWith(" NEXT"))
                {
                    if (bs_results.Position == bs_results.Count-1) break;
                    bs_results.MoveNext();
                    row = ((DataRowView)bs_results.Current).Row;
                    myMergeField.Code.Text = String.Empty;
                }

                if (fieldtext.Contains("firstname"))
                {
                    myMergeField.Select();
                    oWord.Selection.TypeText(row["firstName"].ToString());
                }
                if (fieldtext.Contains("lastname"))
                {
                    myMergeField.Select();
                    oWord.Selection.TypeText(row["lastName"].ToString());
                }
                if (fieldtext.Contains("cellphone"))
                {
                    myMergeField.Select();
                    oWord.Selection.TypeText(row["phoneCell"].ToString());
                }
            }

This is simple, and to the point, and works well. It has limitations though. It loops merge fields, so there are only so many labels created for example as are propagated in the .dot template file, as apposed to rows in the datatable.

My QUESTION:
How do I either propagate labels based on the row count, or do it completely different??

I basically have a results datatable with all the data I need, and would like to mail merge that data to labels in a Word template.

Any help would be appeciated! PLEASE!