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;
}