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.

Wed Apr 21, 2021 3:18 pm

Hello,

I actually have an issue with mailMerge. I try to merge my data with nested IF like this :

{ IF { MERGEFIELD myField } <> "66" "TRUE" "FALSE" }

I succeed to merge the myField MERGEFIELD and got that result :

{ IF "myField" <> "66" "TRUE "FALSE}

but i can't find a solution for merge the conditional IF MERGEFIELD.
Can you help me please ?

Thanks a lot. :D

converterRTF
 
Posts: 2
Joined: Wed Apr 21, 2021 9:20 am

Thu Apr 22, 2021 4:01 am

Hello,

Thanks for your inquiry!

I created a field “{ IF { MERGEFIELD myField } <> "66" "TRUE" "FALSE" }” to the Word file and mail merge it with the latest Spire.Doc V9.4.12. Both the IF field and the Merge field can merge correctly. I also attached my testing code for your reference.

Code: Select all
            //{ IF { MERGEFIELD myField } <> "66" "TRUE" "FALSE" }
            Document newDoc = new Document();
            Section section = newDoc.AddSection();
            Paragraph paragraph = section.AddParagraph();

            //The  IF field
            IfField ifField = new IfField(newDoc);
            ifField.Type = FieldType.FieldIf;
            ifField.Code = "IF ";
            paragraph.ChildObjects.Add(ifField);
           
            //the merge field
            MergeField mergeField = new MergeField(newDoc);
            mergeField.Type = FieldType.FieldMergeField;
            mergeField.Code = "MERGEFIELD myField";
            mergeField.FieldText = "myField";
            paragraph.ChildObjects.Add(mergeField);

            TextRange textRange = new TextRange(newDoc);
            textRange.Text = "<> \"66\" \"TRUE\" \"FALSE\" ";
            paragraph.ChildObjects.Add(textRange);

            FieldMark mergeFieldMark = new FieldMark(newDoc, FieldMarkType.FieldEnd);
            mergeField.End = mergeFieldMark;
            paragraph.ChildObjects.Add(mergeFieldMark);

            FieldMark ifFieldMark = new FieldMark(newDoc, FieldMarkType.FieldEnd);
            ifField.End = ifFieldMark;
            paragraph.ChildObjects.Add(ifFieldMark);

            //save template
            string result = "myField.docx";
            newDoc.SaveToFile(result, FileFormat.Docx2013);

            //merge
            Document doc = new Document();
            doc.LoadFromFile(result);
            var FieldNames = new string[] { "myField" };
            var FieldValues = new string[] { "aaa" };
            doc.MailMerge.Execute(FieldNames, FieldValues);

            doc.IsUpdateFields = true;

            result = "myFieldResult.docx";
            doc.SaveToFile(result, FileFormat.Docx2013);


If you were not using the latest version, I recommend that you can try the latest version with my code again. If the issue still exists, please provide us with your target framework, system information (E.g. Win7, 64 bit) and region setting (E.g. China, Chinese) for reference. Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

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

Thu Apr 22, 2021 8:04 am

Hello Marcia,

Thanks for your reply. I'm sorry I didn't specify that I'm actually working in JAVA with my company not .NET is it still possible to do the same operation with the JAVA Framework ?

Also and very important I actually have nested IF in my code. IF in IF and sometimes i have 3 IF nested...
Do you think in JAVA it is possible to manage that situation ?

Code: Select all

{ IF { MERGEFIELD field1 } <> "062" " { IF { MERGEFIELD field2} <> "063" "TRUE"  ""} " "FALSE" }



Thanks again for you reply
regards

converterRTF
 
Posts: 2
Joined: Wed Apr 21, 2021 9:20 am

Thu Apr 22, 2021 10:59 am

Hello,

Thanks for your feedback!

Our Spire.Doc for JAVA supports the same operation. Here I attached the JAVA code that can realize the field you need and mail merge in it for your reference.

AddField.zip

If you encounter any issues, just feel free to contact us.

Sincerely,
Marcia
E-iceblue support team
User avatar

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

Fri May 07, 2021 9:28 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