Mail Merge Program


This is a simple mail merge program. This program reads from 3 different text files (by default) and merge all the info to produce mail documents. The mail documents produced will be named as 1.doc, 2.doc, 3.doc ...etc. It will not overwrite any existing files with the same name in the directory.

The contents of the text files are as follows ;

theTags.txt - This file contains only the recognised tags
theList.txt - This file contains the user info in "key:value" format
theTemplate.txt - This file is the template to be used
MailMerge.cs - The program

I have also provide extra text files for those who are interested. They are :

theTags1.txt
theList1.txt
theTemplate1.txt


Best wishes and good luck !

Source Code : 

[theTags.txt]
name
age
city
tel
shoe
[theList.txt]
name:Alan
age:23
city:Auckland
tel:12341
shoe:8
name:Ken
age:33
city:Auckland
tel:
shoe:8
name:James
age:74
city:Tauranga
tel:18564
shoe:18
name:Liz
age:54
city:Hamilton
tel:34566
shoe:5
[theTemplate.txt]
Dear <>
This
is your info :
Age = <>
City = <>
Tel = <>
Gender = <>
Shoe Size = <>
regards
saygin
[MailMerge.cs]
/*
Author : Say Gin Teh
Date : 15/3/2001
Copyright
Free to distribute this program only for educational purpose
To Compile (Alternatively use compile.bat):
csc /t:exe /r:System.Drawing.DLL;System.WinForms.dll;
Microsoft.Win32.Interop.DLL;System.dll /out:SGMailMerge.exe
MailMerge.cs
To Run:
SGMailMerge.exe
*/
namespace SGMailMerge
{
using System;
using System.Drawing;
using System.ComponentModel;
using System.WinForms;
using System.IO;
using System.Collections;
using System.Text;
///
/// Summary description for MailMerge.
///
public class MailMerge : System.WinForms.Form
{
///
/// Required by the Win Forms designer
///
private System.ComponentModel.Container components;
private System.WinForms.RichTextBox richTextBox1;
private System.WinForms.Button button3;
private System.WinForms.Button button2;
private System.WinForms.Button button1;
private System.WinForms.Label label2;
private System.WinForms.Label label1;
private System.WinForms.TextBox textBox1;
private System.WinForms.TextBox textBox2;
private System.WinForms.Label label3;
private System.WinForms.TextBox textBox3;
private System.WinForms.GroupBox groupBox1;
private Stack tags = new Stack();
private int tagCount = 0;
private string startTag = "<<";
private string endTag = ">>";
private File tagFile;
private File listFile;
private File templateFile;
private Hashtable hashtable = new Hashtable();
private int x = 0;
private StringBuilder sb = new StringBuilder();
private StringBuilder tempSb = new StringBuilder();
public MailMerge()
{
InitializeComponent();
}
///
/// Clean up any resources being used
///
public override void Dispose()
{
base.Dispose();
components.Dispose();
}
///
/// The main entry point for the application.
///
public static void Main(string[] args)
{
Application.Run(
new MailMerge());
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with an editor
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.richTextBox1 = new System.WinForms.RichTextBox();
this.groupBox1 = new System.WinForms.GroupBox();
this.label3 = new System.WinForms.Label();
this.label2 = new System.WinForms.Label();
this.button1 = new System.WinForms.Button();
this.textBox1 = new System.WinForms.TextBox();
this.textBox3 = new System.WinForms.TextBox();
this.textBox2 = new System.WinForms.TextBox();
this.label1 = new System.WinForms.Label();
this.button3 = new System.WinForms.Button();
this.button2 = new System.WinForms.Button();
//@design this.TrayHeight = 0;
//@design this.TrayLargeIcon = false;
//@design this.TrayAutoArrange = true;
richTextBox1.ReadOnly = true;
richTextBox1.Size =
new System.Drawing.Size(312, 88);
richTextBox1.TabIndex = 10;
richTextBox1.ScrollBars =
System.WinForms.RichTextBoxScrollBars.Vertical;
richTextBox1.Location =
new System.Drawing.Point(16, 136);
groupBox1.Location =
new System.Drawing.Point(16, 16);
groupBox1.TabIndex = 6;
groupBox1.TabStop =
false;
groupBox1.Font =
new System.Drawing.Font("Tahoma", 8f);
groupBox1.Text = "Mail Merge Info";
groupBox1.Size =
new System.Drawing.Size(352, 240);
label3.Location =
new System.Drawing.Point(24, 104);
label3.Text = "Tags file";
label3.Size =
new System.Drawing.Size(56, 24);
label3.TabIndex = 4;
label2.Location =
new System.Drawing.Point(24, 64);
label2.Text = "List file";
label2.Size =
new System.Drawing.Size(56, 24);
label2.TabIndex = 2;
button1.Location =
new System.Drawing.Point(24, 272);
button1.Size =
new System.Drawing.Size(75, 23);
button1.TabIndex = 7;
button1.Text = "Clear";
button1.Click +=
new System.EventHandler(button1_Click);
textBox1.Location =
new System.Drawing.Point(104, 24);
textBox1.Text = "theTemplate.txt";
textBox1.TabIndex = 0;
textBox1.Size =
new System.Drawing.Size(184, 20);
textBox3.Location =
new System.Drawing.Point(104, 104);
textBox3.Text = "theTags.txt";
textBox3.TabIndex = 5;
textBox3.Size =
new System.Drawing.Size(184, 20);
textBox2.Location =
new System.Drawing.Point(104, 64);
textBox2.Text = "theList.txt";
textBox2.TabIndex = 3;
textBox2.Size =
new System.Drawing.Size(184, 20);
label1.Location =
new System.Drawing.Point(24, 24);
label1.Text = "Template file";
label1.Size =
new System.Drawing.Size(71, 16);
label1.TabIndex = 1;
button3.Location =
new System.Drawing.Point(256, 272);
button3.Size =
new System.Drawing.Size(75, 23);
button3.TabIndex = 9;
button3.Text = "Execute";
button3.Click +=
new System.EventHandler(button3_Click);
button2.Location =
new System.Drawing.Point(136, 272);
button2.Size =
new System.Drawing.Size(75, 23);
button2.TabIndex = 8;
button2.Text = "Exit";
button2.Click +=
new System.EventHandler(button2_Click);
this.Text = "Say Gin\'s Mail Merge Program";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(392, 309);
this.Controls.Add(button3);
this.Controls.Add(button2);
this.Controls.Add(button1);
this.Controls.Add(groupBox1);
groupBox1.Controls.Add(richTextBox1);
groupBox1.Controls.Add(label2);
groupBox1.Controls.Add(label1);
groupBox1.Controls.Add(textBox1);
groupBox1.Controls.Add(textBox2);
groupBox1.Controls.Add(label3);
groupBox1.Controls.Add(textBox3);
}
// Clear button pressed - ok
protected void button1_Click(object sender, System.EventArgs e)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
richTextBox1.Text = "";
}
// Exit button pressed - ok
protected void button2_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Closing down ...
Hope you enjoyed
using Say Gin's MailMerge");
Dispose();
Application.Exit();
}
// Execute button pressed
protected void button3_Click(object sender, System.EventArgs e)
{
richTextBox1.Text = "";
if (!Validate_TextField() || !Check_if_exist())
{
return;
}
/*Ok to proceed
Setup the recognised tags
Read from list file then read from template
and then write to a new document
*/
try
{
Setup_tags();
Read_Write_list();
}
catch(Exception ex)
{
richTextBox1.Text = "Problem encountered\n";
richTextBox1.Text += "=> "+ex.ToString();
resetAll();
}
}
/*Validates the input*/
private bool Validate_TextField()
{
bool valid = true;
if (textBox1.Text.Trim().Length ==0)
{
richTextBox1.Text += "The template file name is blank\n";
valid =
false;
}
if (textBox2.Text.Trim().Length ==0 )
{
richTextBox1.Text += "The list file name is blank\n";
valid =
false;
}
if (textBox3.Text.Trim().Length ==0)
{
richTextBox1.Text += "The tag file name is blank\n";
valid =
false;
}
if(!valid)
{
richTextBox1.Text += "Blank spaces not allowed\n";
}
else
{
templateFile =
new File(textBox1.Text);
listFile =
new File(textBox2.Text);
tagFile =
new File(textBox3.Text);
}
return valid;
}
/*Check for files*/
private bool Check_if_exist()
{
bool exist = true;
if (!templateFile.Exists)
{
richTextBox1.Text +=
"File "+templateFile.ToString()+" does not exists"+"\n";
exist =
false;
}
if (!listFile.Exists)
{
richTextBox1.Text +=
"File "+listFile.ToString()+" does not exists"+"\n";
exist =
false;
}
if (!tagFile.Exists)
{
richTextBox1.Text +=
"File "+tagFile.ToString()+" does not exists"+"\n";
exist =
false;
}
if(!exist)
{
richTextBox1.Text +=
"Please re-enter new file name(s)"+"\n";
}
return exist;
}
private void Setup_tags()
{
StreamReader sr = (StreamReader) tagFile.OpenText();
string s = "";
while((s = sr.ReadLine())!=null)
{
if(s.Trim().Length>0)
{
tags.Push(s);
}
}
tagCount = tags.Count;
sr.Close();
}
private void Read_Write_list()
{
richTextBox1.Text = "Process completed. Files produced are :\n";
StreamReader sr = (StreamReader) listFile.OpenText();
string s = "";
// Fill up the hashtable
while((s = sr.ReadLine())!=null)
{
/*Keep adding user key-value to hashtable*/
if(s.Trim().Length>0)
{
if(!hashtable.Contains
(s.Substring(0,s.IndexOf(":"))))
{
/*continue to store current user info*/
hashtable.Add(s.Substring
(0,s.IndexOf(":")),s.Substring(s.IndexOf(":")+1));
}
else
{
/*when next user encountered*/
if(!finalCheckBeforePrinting())
{
sr.Close();
return;
}
hashtable.Add
(s.Substring(0,s.IndexOf(":")),s.Substring(s.IndexOf(":")+1));
}
}
}
/* print the last person in the list*/
finalCheckBeforePrinting();
sr.Close();
tags.Clear();
}
private void Read_Write_template()
{
StreamReader sr = (StreamReader) templateFile.OpenText();
string s = "";
while((s = sr.ReadLine())!=null)
{
/*Build up all info for this particular person*/
sb.Append(FillTag(s)+"\n");
}
x = x +1;
/* If a file with same name already exist
- do not overwrite it*/
if (!new File(x+".doc").Exists)
{
richTextBox1.Text += x+".doc\n";
FileStream output =
new FileStream(x+".doc",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw =
new StreamWriter(output);
sw.BaseStream.Seek(0, SeekOrigin.End);
sw.Write(sb.ToString());
sw.Flush();
sw.Close();
}
else
{
richTextBox1.Text +=
"File "+x+".doc already exist - NOT overiding it\n";
}
sb.Remove(0,sb.Length);
sr.Close();
}
/* Builds a line at a time*/
private string FillTag(string s)
{
tempSb.Remove(0,tempSb.Length);
while(s.Length!=0)
{
int pos = s.IndexOf(startTag);
if(pos < 0)
{
tempSb.Append(s);
break;
}
tempSb.Append(s.Substring(0,pos));
// first part befor any start tag
s = s.Substring(pos);
pos = s.IndexOf(endTag);
if (pos<0)
{
tempSb.Append(s);
// if a string contains extra starttag
break;
}
if(hashtable.ContainsKey(s.Substring(0+2,pos-2)))
{
tempSb.Append((
string)
hashtable[s.Substring(0+2,pos-2)]);
}
else
{
tempSb.Append("< >");
}
s = s.Substring(pos+2);
}
return tempSb.ToString();
}
private bool finalCheckBeforePrinting()
{
/*If the list contains unrecognisable tags -
reset all and start all over again*/
if(hashtable.Count > tags.Count)
{
richTextBox1.Text = "Problem encountered\n";
richTextBox1.Text += "=>In the \"List file\" - each person \n";
richTextBox1.Text += " have more tags assigned than the\n";
richTextBox1.Text += " recognised tags in the \"Tags file\"";
resetAll();
return false;
}
/* print the letter for one person only*/
Read_Write_template();
hashtable.Clear();
return true;
}
private void resetAll()
{
tags.Clear();
tagCount = 0;
tagFile =
null;
listFile =
null;
templateFile =
null;
hashtable.Clear();
x = 0;
sb.Remove(0,sb.Length);
tempSb.Remove(0,tempSb.Length);
}
}
}


Similar Articles