Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Dec 09, 2021 9:02 am

If I have a large block of text (headline, paragraphs including mergefields) that has to be removed conditionally in mailmerge, what's the best approach?
To be more precise: assuming the structure below I want to remove #Heading 2, #Paragraph 4 and #Paragraph 5 depending on the data merged.
#Heading 1
#Paragraph 1
#Paragraph 2
#Paragraph 3
#Heading 2
#Paragraph 4
#Paragraph 5
#Heading 3
#Paragraph 6
#Paragraph 7
#Paragraph 8

gfischer
 
Posts: 7
Joined: Thu Nov 04, 2021 9:34 am

Thu Dec 09, 2021 10:01 am

Hello,

Thanks for your inquiry!

Do you use the style of the paragraph to identify if the paragraph needs to be deleted?

If so, please refer to the following code to use MergeFieldEventHandler to achieve your needs of remove #Heading 2. Here I also attached my input file. But I do not understand how you judge the #Paragraph 4 and #Paragraph 5. To help us solve your issue quickly and efficiently, please provide us your input file for reference, thanks in advance.
Code: Select all
           Document document = new Document();
            document.LoadFromFile(@"E:\testdoc\doc3.docx");

            List<string> FieldName = new List<string>();
            FieldName.Add("first");
            FieldName.Add("second");
            FieldName.Add("third");
            FieldName.Add("fourth");
            FieldName.Add("fifth");

            List<string> values = new List<string>();
            values.Add("aaa");
            values.Add("bbb");
            values.Add("ccc");
            values.Add("ddd");
            values.Add("eee");

            document.MailMerge.MergeField += new MergeFieldEventHandler(MailMerge_MergeField);

            void MailMerge_MergeField(object sender, MergeFieldEventArgs args1)
            {
                Console.WriteLine(args1.CurrentMergeField.OwnerParagraph.StyleName);
                if(args1.CurrentMergeField.OwnerParagraph.StyleName.Equals("Heading1"))
                {
                    args1.CurrentMergeField.OwnerParagraph.OwnerTextBody.ChildObjects.Remove(args1.CurrentMergeField.OwnerParagraph);
                }
            }
            document.MailMerge.Execute(FieldName.ToArray(), values.ToArray());
            Console.ReadKey();
            document.SaveToFile("after remove.docx");


If I have any misunderstand, please provide us with your input file, and you expect output file for reference.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Thu Dec 09, 2021 10:21 am

Sorry for not being clear enough about what I want to achive. I append a zip file with input.docx and result.docx. I've marked the bunch of text that I want to remove conditionally in the input.docx file.

gfischer
 
Posts: 7
Joined: Thu Nov 04, 2021 9:34 am

Fri Dec 10, 2021 5:40 am

Hello,

Thanks for sharing more information!

Please refer to the following code to achieve your needs. Here I also attached my test data.

Code: Select all
           Document document = new Document();
            document.LoadFromFile(@"E:\testdoc\sample (1)\input.docx");


            document.MailMerge.MergeGroup += new MergeGroupEventHandler(MailMerge_MergeField);
            void MailMerge_MergeField(object sender, MergeGroupEventArgs args1)
            {

                if (args1.GroupName.Equals("CriticalPointsTable") && args1.EventType.Equals(GroupEventType.TableStart))
                {
                    //delete the paragraph before the CriticalPointsTable's tablestart
                    args1.MergeField.OwnerParagraph.PreviousSibling.Owner.ChildObjects.Remove(args1.MergeField.OwnerParagraph.PreviousSibling);
                }
                if (args1.GroupName.Equals("CriticalPointsTable") && args1.EventType.Equals(GroupEventType.TableEnd))
                {
                    //delete the table before the CriticalPointsTable's tableend
                    args1.MergeField.OwnerParagraph.PreviousSibling.Owner.ChildObjects.Remove(args1.MergeField.OwnerParagraph.PreviousSibling);
                    //delete the para before the table
                    args1.MergeField.OwnerParagraph.PreviousSibling.Owner.ChildObjects.Remove(args1.MergeField.OwnerParagraph.PreviousSibling);
                }
            }
            DataSet dsData = new DataSet();
            dsData.ReadXml(@"E:\\testdoc\\Data.xml");

            List<DictionaryEntry> list = new List<DictionaryEntry>();
            DictionaryEntry dictionaryEntry = new DictionaryEntry("Request", string.Empty);
            list.Add(dictionaryEntry);

            dictionaryEntry = new DictionaryEntry("CriticalPointsTable", "Request_Id = %Request.Request_Id%");
            list.Add(dictionaryEntry);

            dictionaryEntry = new DictionaryEntry("CriticalPointsRow", "CriticalPointsTable_Id = %CriticalPointsTable.CriticalPointsTable_Id%");
            list.Add(dictionaryEntry);

            document.MailMerge.ExecuteWidthNestedRegion(dsData, list);
            document.SaveToFile("after remove.docx");


Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Tue Jan 04, 2022 5:53 am

Hello,

Hope you are doing well!

Has the issue been solved now? Could you please give us some feedback at your convenience?

Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.Doc