none
How to remove empty value mail merge fields? RRS feed

  • Question

  • Hi,

    I am trying to generate mail merge document using open xml. I can generate the document but if there was no value found for a merge field, it leaves an empty space. If there is no value found for a mail merge field I would like to remove/delete it so the finally generated document does not have any blank spaces.

    Regards

    Thursday, September 18, 2014 2:11 PM

All replies

  • Hi,

    >>I can generate the document but if there was no value found for a merge field, it leaves an empty space. If there is no value found for a mail merge field I would like to remove/delete it so the finally generated document does not have any blank spaces.<<

    Based on the description, you want to remove the blank spaces when you using Open XML to mail merge. To achieve the goal, we can remove the maile merge filed if the data is null or empty rather than set its value to empty.

    Woule you mind sharing the code you are using to mail merge? And here is blog about Mail Merging:
    Mail Merging with a Custom Client Using the Open XML SDK 2.0

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, September 19, 2014 2:27 AM
    Moderator
  • Well you are right that we can remove the mail merge filed if the data is null or empty rather than set its value to empty.

    I have tried that but I am not able to remove the merge fields.

    Could you please give me an example?

    Tuesday, October 14, 2014 11:15 AM
  • Hi KBIN,

    >>Could you please give me an example?<<

    Yes, we can modify the code sample metioned above to acheve the goal.

    Orignal Code in the sample(MailMerger Class):

       // Replace all merge fields with Data
                        foreach (XElement field in mailMergeFields)
                        {
                            string fieldName = field.Attribute(XMLNS + "instr").Value.Replace("MERGEFIELD", string.Empty).Trim();
                            if (row.Table.Columns.Contains(fieldName))
                            {
                                XElement newElement = field.Descendants(XMLNS + "r").First();
                                newElement.Descendants(XMLNS + "t").First().Value = row.Field<string>(fieldName);
                                field.ReplaceWith(newElement);
                            }
                        }

    Modify the code like below:

     // Replace all merge fields with Data
                        foreach (XElement field in mailMergeFields)
                        {
                            string fieldName = field.Attribute(XMLNS + "instr").Value.Replace("MERGEFIELD", string.Empty).Trim();
                            if (row.Table.Columns.Contains(fieldName))
                            {
                                XElement newElement = field.Descendants(XMLNS + "r").First();
                                if (String.IsNullOrEmpty(row.Field<string>(fieldName)))
                                {
                                    field.Remove();
                                }
                                else {
                                    newElement.Descendants(XMLNS + "t").First().Value = row.Field<string>(fieldName);
    
                                    field.ReplaceWith(newElement);
                                }
                              
                            }
                        }

    In addtion, the sample code was using build on .Net framwork 3.5 and Open XML SDK 2.0. If you are using Open XML SDK 2.5, you need to use Microsoft .NET Framework version 4.0. or above.

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, October 15, 2014 7:34 AM
    Moderator