Click or drag to resize

MailMergeClearOptions Enumeration

Specifies options that determine what items are removed during mail merge.

Namespace:  SautinSoft.Document.MailMerging
Assembly:  SautinSoft.Document (in SautinSoft.Document.dll) Version: 4.0.11.12 (4.0.11.12)
Syntax
[FlagsAttribute]
public enum MailMergeClearOptions
Members
  Member nameValueDescription
None0 Do not remove unused fields nor empty ranges nor empty paragraphs.
RemoveUnusedFields1 Remove fields for which no data has been found in the mail merge data source.
RemoveEmptyRanges2 Remove ranges into which no field has been merged.
RemoveEmptyParagraphs4 Remove paragraphs which contained merge fields but none of them has been merged.
RemoveEmptyTableRows8 Remove table rows which contained merge fields but none of them has been merged.
Examples
Shows how use ClearOptions - remove specific elements if no data has been imported into them in C#
using SautinSoft.Document;
using SautinSoft.Document.MailMerging;

namespace Sample
{
    class Sample
    {
        static void Main(string[] args)
        {
            MailMergeWithClearOptions();
        }
        /// <summary>
        /// Shows how use ClearOptions - remove specific elements if no data has been imported into them.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/examples/mail-merge-clear-options-csharp-vb.php
        /// </remarks>
        static void MailMergeWithClearOptions()
        {
            DocumentCore document = DocumentCore.Load(@"..\..\MailMergeClearOptions.docx");

            // Example 1: Remove fields for which no data has been found in the mail merge data source.
            var dataSource1 = new
            {
                Field2 = "Some text"
            };
            document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveUnusedFields;
            document.MailMerge.Execute(dataSource1, "Example1");


            // Example 2: Remove paragraphs contained merge fields but none of them has been merged.
            var dataSource2 = new
            {
                Field1 = string.Empty,
                Field2 = "Some text"
            };

            document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyParagraphs;
            document.MailMerge.Execute(dataSource2, "Example2");


            // Example 3: Remove table rows contained merge fields but none of them has been merged.
            var dataSource3 = new
            {
                Field1 = "Some text 1",
                Field2 = (string)null,
                Field3 = "Some text 3",
            };

            document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyTableRows;
            document.MailMerge.Execute(dataSource3, "Example3");


            // Example 4: Remove ranges into which no field has been merged.
            var dataSource4 = new
            {
                TotalRecords = 2,
                Records = new object[]
                {
                    new
                    {
                        Record = 1,
                        Text = "Some text 1"
                    },
                    new
                    {
                        Record = (object)null,
                        Text = string.Empty
                    },
                    new
                    {
                        Record = 3,
                        Text = "Some text 3"
                    },
            }
            };
            document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyRanges;
            document.MailMerge.Execute(dataSource4, "Example4");

            string resultPath = "ClearOptions.docx";

            // Save the output to file.
            document.Save(resultPath);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath) { UseShellExecute = true });
        }
    }
}
Shows how use ClearOptions - remove specific elements if no data has been imported into them in VB.Net
Option Infer On

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks

Imports SautinSoft.Document
Imports SautinSoft.Document.MailMerging

Namespace Sample
    Friend Class Program
        Shared Sub Main(ByVal args() As String)
            MailMergeWithClearOptions()
        End Sub
        ''' <summary>
        ''' Shows how use ClearOptions - remove specific elements if no data has been imported into them.
        ''' </summary>
        ''' <remarks>
        ''' Details: https://sautinsoft.com/products/document/examples/mail-merge-clear-options-csharp-vb.php
        ''' </remarks>
        Private Shared Sub MailMergeWithClearOptions()
            Dim document As DocumentCore = DocumentCore.Load("..\MailMergeClearOptions.docx")

            ' Example 1: Remove fields for which no data has been found in the mail merge data source.
            Dim dataSource1 = New With {Key .Field2 = "Some text"}
            document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveUnusedFields
            document.MailMerge.Execute(dataSource1, "Example1")


            ' Example 2: Remove paragraphs contained merge fields but none of them has been merged.
            Dim dataSource2 = New With {
                Key .Field1 = String.Empty,
                Key .Field2 = "Some text"
            }

            document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyParagraphs
            document.MailMerge.Execute(dataSource2, "Example2")


            ' Example 3: Remove table rows contained merge fields but none of them has been merged.
            Dim dataSource3 = New With {
                Key .Field1 = "Some text 1",
                Key .Field2 = DirectCast(Nothing, String),
                Key .Field3 = "Some text 3"
            }

            document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyTableRows
            document.MailMerge.Execute(dataSource3, "Example3")


            ' Example 4: Remove ranges into which no field has been merged.
            Dim dataSource4 = New With {
                Key .TotalRecords = 2,
                Key .Records = New Object() {
                    New With {
                        Key .Record = 1,
                        Key .Text = "Some text 1"
                    },
                    New With {
                        Key .Record = DirectCast(Nothing, Object),
                        Key .Text = String.Empty
                    },
                    New With {
                        Key .Record = 3,
                        Key .Text = "Some text 3"
                    }
                }
            }
            document.MailMerge.ClearOptions = MailMergeClearOptions.RemoveEmptyRanges
            document.MailMerge.Execute(dataSource4, "Example4")

            Dim resultPath As String = "ClearOptions.docx"

            ' Save the output to file.
            document.Save(resultPath)

            ' Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(resultPath) With {.UseShellExecute = True})
        End Sub
    End Class
End Namespace
See Also