
In this final step, editing functions will be added to the mail merge program.
Loading, Saving and Importing Files
Steps 1 and 2 of the program automatically loaded a sample file named template.tx. There was no way to load a different file, or to save changes made to its contents. The code for loading a file therefore needs to be changed so that it opens a dialog box where the user can select a file, and menu items need to be added for saving a file, and for importing files from other word processor formats like RTF and HTML. Note that files need to be saved in TX Text Control format, so that the text fields are preserved. A customized SaveFile dialog is used to prevent users from accidently saving to a different file format.
[C#]
private void mnuFile_SaveTemplateAs_Click(object sender, System.EventArgs e)
{
dlgSaveFile.Filter = "Text Control Files (*.tx)|*.tx";
dlgSaveFile.ShowDialog();
if (dlgSaveFile.FileName != "")
textControl1.Save(dlgSaveFile.FileName,
TXTextControl.StreamType.InternalFormat);
}
[Visual Basic]
Private Sub mnuTemplate_SaveAs_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuTemplate_SaveAs.Click
With dlgSaveFile
.Filter = "Text Control Files|*.tx"
.ShowDialog()
If .FileName <> "" Then TextControl1.Save(.FileName, _
TXTextControl.StreamType.InternalFormat)
End With
End Sub
[Delphi]
procedure Form1.mnuFile_SaveTemplateAs_Click(sender: System.Object;
e: System.EventArgs);
begin
with dlgSaveFile do begin
Filter := 'Text Control Files (*.tx)|*.tx';
ShowDialog;
if FileName <> '' then TextControl1.Save(FileName,
TXTextControl.StreamType.InternalFormat);
end;
end;
Adding Database Fields
The sample file used in steps 1 and 2 already contained all required text fields. For creating a file from scratch, however, the program must provide a way to select fields from a database and insert them into the document.
This is done with an Insert menu which contains items corresponding to database columns:

As different databases may contain different column names, the Insert menu must be created dynamically. For each member of the database's Columns collection, an item is added to the menu. An event handler is provided which, when one of the menu items is clicked, will insert a corresponding text field into the document:
[C#]
private void CreateTextFieldMenu()
{
mnuInsert.MenuItems.Clear();
foreach (DataColumn DataField in dsAddress.Tables[0].Columns)
mnuInsert.MenuItems.Add(DataField.ColumnName,
new EventHandler(InsertMenuItems_Click));
}
private void InsertMenuItems_Click(object sender, System.EventArgs e)
{
TXTextControl.TextField textField = new TXTextControl.TextField();
textField.Text = "(" + ((MenuItem)sender).Text + ")";
textField.Name = ((MenuItem)sender).Text;
textField.ShowActivated = true;
textField.DoubledInputPosition = true;
textControl1.TextFields.Add(textField);
}
[Visual Basic]
Private Sub CreateTextFieldMenu()
Dim DataField As DataColumn
For Each DataField In dsAddress.Tables(0).Columns
mnuInsert.MenuItems.Add(DataField.ColumnName, _
New EventHandler(AddressOf InsertMenuItems_Click))
Next
End Sub
Protected Sub InsertMenuItems_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
Dim textField As New TXTextControl.TextField
textField.Text = "(" + sender.text + ")"
textField.Name = sender.text
textField.ShowActivated = True
textField.DoubledInputPosition = True
TextControl1.TextFields.Add(textField)
End Sub
[Delphi]
procedure Form1.CreateTextFieldMenu();
var
DataField : DataColumn;
begin
mnuInsert.MenuItems.Clear;
for DataField in dsAddress.Tables[0].Columns do begin
mnuInsert.MenuItems.Add(DataField.ColumnName, InsertMenuItems_Click);
end;
end;
procedure Form1.InsertMenuItems_Click(sender : System.Object;
e : System.EventArgs);
var
textField : TXTextControl.TextField;
begin
textField := TXTextControl.TextField.Create;
textField.Text := '(' + System.Windows.Forms.MenuItem(sender).Text + ')' ;
textField.Name := sender.ToString;
textField.ShowActivated := True;
textField.DoubledInputPosition := True;
TextControl1.TextFields.Add(textField);
end;
Selecting a Database
At the bottom of the screen, a text box has been inserted which shows the name of an address database file. The box will initially be empty, meaning that no database is connected to the document. Clicking on the button to the right of the text box will launch a dialog box in which a database file can be selected.