Free Support Forum - aspose.com

Merge and "<<Next Record>>-" showing for emty records - want to remove them

I am a programmer working with VS C# and some code I inherited to create mailing and folder labels from data in a SQL Server database using Mail Merge. Another person created the MS Word documents that we are using.

Everything works fine, but when there are fewer data records than labels on a sheet, we get "<>." printed at the top of each label instead of everything just being blank.

We need to find a way to prevent this from happening.

Thanks!

Bill

Here is the code from a module:

protected void Page_Load(object sender, EventArgs e)

{

// Define the Aspose.Words license

Aspose.Words.License license = new Aspose.Words.License();

license.SetLicense("../Bin/Aspose.Words.lic");

// Clear the Gridview data source

gvAdmitLetter.DataSource = null;

gvAdmitLetter.DataBind();

}

protected void btnCreateLetter_Click(object sender, EventArgs e)

{

// Open the file chosen by the user

System.IO.Stream ioStream = File1.PostedFile.InputStream;

int fileLength = File1.PostedFile.ContentLength;

byte[] buffer = new byte[fileLength];

ioStream.Read(buffer, 0, fileLength);

MemoryStream stream = new MemoryStream(buffer);

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)dsrcAdmitLetter.Select(DataSourceSelectArguments.Empty);

try

{

// Do the mail merge from the DataVeiew to the document

doc.MailMerge.Execute(dv.ToTable());

// 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

Thank you for your interest in Aspose.Words. Could you please attach your template for testing? (only you and Aspose staff can download it). I will investigate this and provide you more information.

Best regards.

Thank you. Please see the attached template file.

Bill Patterson

UW Madison School of Business

Hi

Thank you for additional information. NEXT field isn’t MERGEFIELD. You can try using the following code to solve your problem.

Document doc = new Document(@"277_106302_wpatterson\in.doc");

doc.MailMerge.Execute(table);

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();

}

doc.Save(@"277_106302_wpatterson\out.doc");

I hope that this will solve your problem.

Best regards.

Thanks so much! That solved our problem!

Bill Patterson

UW Madison School of Business