Question on how to remove pages from a MailMerge


#1

Hello Aspose Team!

I just had a quick question regarding mail merging and labels. I couldn’t find a easy answer in the documentation, so what I am try to do is simply remove extra pages from the document at execution time, say if someone only chooses three or four records. What is happening, basically, is if the user selects say 50 or 60 records (or more than 30, which is the max per page for the label type I am using) it will create two pages (which is fine) and they do their thing. The problem begins when they try to generate another label set with a smaller group, say less than 30. It will keep creating the second page again regardless. I actually went as far as deleting the file from the File System before recreating it, but I still have the same problem.

If you know of any function to remove blank page(s), have any code examples or could steer me in the right direction, it would be really appreciated!

Thanks so much in advance!

Jason


#2

The behavior like you have described is not displayed in our label mail merge tests. Please attach your template together with a code snippet so that we could reproduce the problem and work out some recomendations for you.

Regards,


#3

Hi Vladimir,

Thanks for the quick response! Per your request, here is a copy of the code snippet:

string [] name = new string[list_of_records.Items.Count];
string [] address = new string[list_of_records.Items.Count];
string [] city = new string[list_of_records.Items.Count];
string [] state = new string[list_of_records.Items.Count];
string [] zip = new string[list_of_records.Items.Count];
string sql_query = “”;
int counter = 0;

error_text.Text = “”;
SqlConnection sql_conn = new SqlConnection(ConfigurationSettings.AppSettings[“gearupservices”]);
sql_conn.Open();

for (int i = 0; i < list_of_records.Items.Count; i ++) {

if (list_of_records.ItemsIdea [I].Selected) {

if (person_type == “Student”) {

sql_query = “SELECT firstname + ’ ’ + lastname AS ‘name’,
address, city, state, zip FROM students WHERE personid = '” +
list_of_records.ItemsIdea [I].Value.ToString() + “’”;

}
else if (person_type == “Parent”) {

sql_query = “SELECT firstname + ’ ’ + lastname AS ‘name’,
address, city, state, zip FROM parents WHERE parent_id = '” +
list_of_records.ItemsIdea [I].Value.ToString() + “’”;

}

SqlDataAdapter sql_da = new SqlDataAdapter(sql_query, sql_conn);
DataSet sql_ds = new DataSet();
sql_da.Fill(sql_ds,“selected_records”);

name[counter] = sql_ds.Tables[0].Rows[0][“name”].ToString();
address[counter] = sql_ds.Tables[0].Rows[0][“address”].ToString();
city[counter] = sql_ds.Tables[0].Rows[0][“city”].ToString() + “,”;
state[counter] = sql_ds.Tables[0].Rows[0][“state”].ToString();
zip[counter] = sql_ds.Tables[0].Rows[0][“zip”].ToString();

sql_query = “”;
counter++;

}

}

sql_conn.Close();

DataTable objTable = new DataTable(“table_of_selected_records”);
objTable.Columns.Add(“name”);
objTable.Columns.Add(“address”);
objTable.Columns.Add(“city”);
objTable.Columns.Add(“state”);
objTable.Columns.Add(“zip”);

for (int loop = 0; loop < name.GetLength(0); loop++) {
objTable.Rows.Add(new object[] { name[loop], address[loop], city[loop], state[loop], zip[loop] });
}


//delete the old file so that if they chose a smaller group of students
the file won’t be massive still (this part is not still not shrinking
the file!)


if
(System.IO.File.Exists(“D:\website\staff\gups\reports\mailing_labels_for_”

  • person_type.ToLower() + “" + school + “.doc”)) {

    System.IO.File.Delete("D:\website\staff\gups\reports\mailing_labels_for
  • person_type.ToLower() + “" + school + “.doc”);
    }

    //create the document
    Document objDoc = new Document(“D:\website\staff\gups\reports\input_for_mailing_labels.doc”);

    // Field values from the table are inserted into the mail merge fields found in the document.
    objDoc.MailMerge.Execute(objTable);

    //Delete any extra fields
    objDoc.MailMerge.DeleteFields();

    //save the output
    objDoc.Save("D:\website\staff\gups\reports\mailing_labels_for
    ” + person_type.ToLower() + “" + school + “.doc”);

    error_text.Text = "Mailing Labels Created! Click ";
    link_to_file.Text = “HERE”;

    link_to_file.NavigateUrl =
    "https://www.gearup.mass.edu/reports/mailing_labels_for
    ” +
    person_type.ToLower() + “_” + school + “.doc”;

    Also, I have attached a copy of the template file: input_for_mailing_labels.doc.

    Thanks so much for your help thus far and please let me know what you find out!

    Jason

#4

The file was not attached. Please reattach it. Do not use post preview because it somehow prevents files to be attached.

Regards,


#5

Hi Vladimir,

I actually found the problem today finally, it was just a coding error. The piece of code that was the problem was as follows:

for (int loop = 0; loop < name.GetLength(0); loop++) {
objTable.Rows.Add(new object[] { name[loop], address[loop], city[loop], state[loop], zip[loop] });
}

What was happening was that even though only a few of the array elements had been populated, it would still create empty spaces for the rest of the records in the dataset when creating the mail merge document. What I had to do was change the code like so:

for (int loop = 0; loop < name.Length; loop++) {
if (name[loop] != null) {
objTable.Rows.Add(new object[] { name[loop], address[loop], city[loop], state[loop], zip[loop] });
}
}

That way it only creates a row for the mail merge if the array element contained any data.

Thanks again for your help Vladimir and I’ll be sure to check my code more closely next time! :slight_smile:

Take care,

Jason