I’m trying to produce mailing labels from a database that has both address1 and address2 fields. The problem I am having is that if address2 is NULL (verified from the data) then a blank line appears in the output document. This is the code we are using and any help would be greatly appreciated. Thanks! Bill
// Remove Empty Paragraphs
doc.MailMerge.RemoveEmptyParagraphs = true;
// Do the mail merge from the DataVeiew to the document
doc.MailMerge.Execute(dv.ToTable());
// Remove Next_Field when document from empty records
ArrayList list = new ArrayList();
NodeCollection collection = doc.GetChildNodes(NodeType.FieldStart, true);
foreach (FieldStart start in collection)
{
if (start.FieldType == FieldType.FieldNext || start.FieldType == FieldType.FieldMergeField)
{
list.Add(start);
}
}
foreach (Node start in list)
{
Node node = start;
while (node.NodeType != NodeType.FieldEnd)
{
node = node.NextSibling;
node.PreviousSibling.Remove();
}
node.Remove();
}
// Strip out merged fields with no data
int i;
string[] names = doc.MailMerge.GetFieldNames();
DocumentBuilder builder = new DocumentBuilder(doc);
for (i = 1; i < names.Length; i++)
{
builder.MoveToMergeField(names[i]);
}
// Save the mail merge document as Out.doc
doc.Save(MyPath + @"\Out.doc");
HttpContext.Current.Response.ContentType = "application/ms-word";
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=Out.doc");
HttpContext.Current.Response.TransmitFile("Out.doc");
Hi
Thanks for your request. The following code should work for you.
// Remove Empty Paragraphs
doc.MailMerge.RemoveEmptyParagraphs = true;
// Do the mail merge from the DataVeiew to the document
doc.MailMerge.Execute(dv.ToTable());
// Delete not merged fields
doc.MailMerge.DeleteFields();
Could you also please attach your template and output document for investigating?
Best regards.
Thank you for responding!
I tried that and it didn’t work. I still get blank lines in the output labels.
Please see the template attached. I can’t seem to attach more than one file.
Thanks!
Document doc = new Document(stream);
string MyPath = HttpContext.Current.Server.MapPath(".");
// Create a DataView via the data source using Program and Term
DataView dv = (DataView dsrcStudentAddresses.Select(DataSourceSelectArguments.Empty);
try
{
// Remove Empty Paragraphs
doc.MailMerge.RemoveEmptyParagraphs = true;
// Do the mail merge from the DataVeiew to the document
doc.MailMerge.Execute(dv.ToTable());
// Delete not merged fields
doc.MailMerge.DeleteFields();
// Remove Next_Field when document from empty records
ArrayList list = new ArrayList();
NodeCollection collection = doc.GetChildNodes(NodeType.FieldStart, true);
foreach (FieldStart start in collection)
{
if (start.FieldType == FieldType.FieldNext || start.FieldType == FieldType.FieldMergeField)
{
list.Add(start);
}
}
foreach (Node start in list)
{
Node node = start;
while (node.NodeType != NodeType.FieldEnd)
{
node = node.NextSibling;
node.PreviousSibling.Remove();
}
node.Remove();
}
// Strip out merged fields with no data
int i;
string[] names = doc.MailMerge.GetFieldNames();
DocumentBuilder builder = new DocumentBuilder(doc);
for (i = 1; i < names.Length; i++)
{
builder.MoveToMergeField(names[i]);
}
// Save the mail merge document as Out.doc
doc.Save(MyPath + @"\Out.doc");
HttpContext.Current.Response.ContentType = "application/ms-word";
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=Out.doc");
HttpContext.Current.Response.TransmitFile("Out.doc");
}
catch (Exception ex)
{
lblErrors.Text = ex.Message;
}
dv = null;
doc = null;
stream = null;
ioStream = null;
Hi
Thanks for additional information. RemoveEmptyParagraphs doesn’t work in your case because line with mergefield “Address2” also contains few white characters. So paragraph is not empty. I changed your template and now all works fine. Please see the following code and the attached documents.
string[] names = { "FirstName", "LastName", "Address1", "Address2", "City", "State", "Zip" };
// Address2 line will be removed
object[] values = { "Alexey", "Noskov", "My addres", null, "City", "State", "Zip" };
// Open template
Document doc = new Document(@"Test206\in.doc");
doc.MailMerge.RemoveEmptyParagraphs = true;
// Execute mail merge
doc.MailMerge.Execute(names, values);
// Save document
doc.Save(@"Test206\out.doc");
Hope this helps
Best regards.
Thanks so very much! That solved the problem.
I had been trying unsuccessfully to add {IF…} syntax to skip the field if it was “” and I must have left behind a few white space spaces.
It works like a charm now!
Thanks again,
Bill