Missing data on merge


#1

I have two questions regarding the merge process:

1 - We would like to be able to indicate clearly to users when merge field data was not available. Could I use the MergeField event to insert some text and format the field? Do you have any suggestions?

2- With address line data, we have from 1 to 4 address lines independent of city, prov/state, postal code etc. Can I create a single address field that includes up to 4 lines with embedded line feed characters?

Thanks for your assistance,

Todd


#2

Hi Todd,

You can either use the MergeField event or add a calculated column to your DataTable to insert special text when field data is not available. If you want to apply special formatting you can do so only in the MergeField event, here is an example:

[Test]
public void TestMailMergeRegionWithDocBuilder()
{
Document doc = TestUtil.Open(@“TestMailMergeRegionSimple.doc”);
doc.MailMerge.MergeField += new MergeFieldEventHandler(HandleMergeFieldEvent);

this.builder = new DocumentBuilder(doc);
DataSet ds = CreateTestDataSet();
doc.MailMerge.ExecuteWithRegions(ds);
doc.Save(@“TestMailMergeRegionWithDocBuilder Out.doc”);
}

///


/// Makes product description blue if it contains the word “Blue”, otherwise makes it red.
///

private void HandleMergeFieldEvent(object sender, MergeFieldEventArgs e)
{
if (e.FieldName == “Description”)
{
builder.MoveToMergeField(e.FieldName);
string fieldValue = e.FieldValue.ToString();
if (fieldValue.IndexOf(“Blue”) != -1)
builder.Font.Color = System.Drawing.Color.Blue;
else
builder.Font.Color = System.Drawing.Color.Red;
//Write the text using the builder, don’t let the mail merge engine to insert text too.
builder.Write(fieldValue);
e.Text = “”;
}
}


#3

Yes, you can insert text with paragraph breaks into merge fields.

Just build up your whole address block in the MergeField event and insert it into one field. Use constants defined in the ControlChar char to separate the lines.

Alternatively, you can leave all address lines as separate merge fields in the document and use the MailMerge.RemoveEmptyParagraphs property to automatically remove empty address lines.


#4

Hi Roman,

Could you please advise how I can get the event handler to work? Here is the test code that I’m calling (I’ve commented out some of the code you sent in order to be able to compile):

using System;
using Aspose.Word;
using System.Data;
using System.IO;

namespace DocGenToolEvaluation
{
///


/// Summary description for Class1.
///

public class CreateTestLetter
{
public void GenerateWithAspose()
{
DataTable MergeData = GetTemplateTable();

Word word = new Word();
Document DocTemplate = word.Open(@“C:\DocGenTest\TestLetter.doc”); Document MergeDoc = DocTemplate.MailMerge.Execute(MergeData);
String FileName = DateTime.Now.ToString(“yyyy-MM-dd–HH-mm-ss-ff”);
MergeDoc.Save(@“C:\DocGenTest\DocGenOutput\Aspose” + FileName + “.doc”);

}
///
/// Makes address blue if it contains the word “Blue”, otherwise makes it red.
///

private void HandleMergeFieldEvent(object sender, MergeFieldEventArgs e)
{
if (e.FieldName == “Address”)
{
//DocumentBuilder builder = new DocumentBuilder(DocTemplate);

//builder.MoveToMergeField(e.FieldName);
string fieldValue = e.FieldValue.ToString();
//if (fieldValue.IndexOf(“Blue”) != -1)
//builder.Font.Color = System.Drawing.Color.Blue;
//else
//builder.Font.Color = System.Drawing.Color.Red;
//Write the text using the builder, don’t let the mail merge engine to insert text too.
//builder.Write(fieldValue);
//DocumentBuilder builder = new DocumentBuilder(DocTemplate);e.Text = “”;
}
}

public CreateTestLetter()
{
//
// TODO: Add constructor logic here
//
}
}
}


#5

Hi,

You need to attach the event handler to the event.

Document DocTemplate = word.Open(@“C:\DocGenTest\TestLetter.doc”);

//This line specifies that HandleMergeFieldEvent should be called for the MergeField event.
DocTemplate.MailMerge.MergeField += new MergeFieldEventHandler(HandleMergeFieldEvent);

Document MergeDoc = DocTemplate.MailMerge.Execute(MergeData)


#6

Thanks for your reply. I’ve hooked up the event, but I have two additional questions:

1 - How do I reference the Document object - i.e. DocTemplate in the example I sent:
DocumentBuilder builder = new DocumentBuilder(DocTemplate);?

2 - How do I set a reference to be able to use System.Drawing?

Thanks again,

Todd


#7

Roman - not to worry - I’ve resolved my problem.


#8

I normally make the Document object a member of the class so you can access it from the methods. But I agree it will be useful to make Document a member of the event args object and I will do this in the next release.