jamesnw
February 20, 2006, 5:04am
1
HI
Is there a way i can force a new line within the text of a maill-merge field?
I have an address, held in the database in html, and it uses to break the line. This gets inserted in a normal mail-merge.
I was wondering what character i can replace the with so that the field :
"Richmond House 79 Whitehall London SW1A 2NS Tel: 020 7210 3000" appears as a proper address format?
Any thoughts?
Thanks - James Whittaker
Here is a sample code:
private void buttonInsertHtmlInMailMerge_Click(object sender, System.EventArgs e)
{
// open file Doc1.doc
string filename = Application.StartupPath + "\\Doc1.doc";
Document doc = new Document(filename);
// start DocumentBuilder for the opened file
builder = new DocumentBuilder(doc);
// add a hadler to MergeField event
doc.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField);
// load some html from file
StreamReader sr = File.OpenText(Application.StartupPath + "\\sometext.html");
string htmltext = sr.ReadToEnd();
sr.Close();
// execute mailmerge
doc.MailMerge.Execute(new string[] {"htmlAddress1"}, new string[] {htmltext});
// save resulting document with a new name
doc.Save(System.IO.Path.GetFileNameWithoutExtension(filename) + "_modified.doc");
}
private DocumentBuilder builder;
private void MailMerge_MergeField(object sender, MergeFieldEventArgs e)
{
if(e.DocumentFieldName.StartsWith("html"))
{
builder.MoveToMergeField(e.DocumentFieldName);
builder.InsertHtml((string)e.FieldValue);
e.Text = "";
}
}
It uses MergeField event to insert html string into the fields with the name starting with 'html'. You need to adapt it slightly to do MailMerge from database.
jamesnw
February 20, 2006, 8:41am
3
Hi
Thanks for the reply. I might be being dopey but i cannot see how to create the event handler for the mailmerge event.
For example, my "doc.MailMerge.[available properties etc list here]" - does not have the mail merge event listed?
Also, do you have any sample vb.net code for adding the event handler/delegate etc?
Thanks in advance.
James
The code for VB.NET will look like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim filename As String = Application.StartupPath + "\\Doc1.doc"
Dim doc As Document = New Document(filename)
builder = New DocumentBuilder(doc)
AddHandler doc.MailMerge.MergeField, AddressOf HandleMergeField
Dim sr As StreamReader = File.OpenText(Application.StartupPath + "\InsertHtml.txt")
Dim htmltext As String = sr.ReadToEnd()
sr.Close()
Dim fields(0) As String
fields(0) = "htmlAddress1"
Dim values(0) As String
values(0) = htmltext
doc.MailMerge.Execute(fields, values)
doc.Save(System.IO.Path.GetFileNameWithoutExtension(filename) + "_modified.doc")
End Sub
Private builder As DocumentBuilder
Private Sub HandleMergeField(ByVal sender As Object, ByVal e As Aspose.Word.Reporting.MergeFieldEventArgs)
If (e.DocumentFieldName.StartsWith("html")) Then
builder.MoveToMergeField(e.DocumentFieldName)
builder.InsertHtml(e.FieldValue)
End If
End Sub