Issue with rendering qoute within If block in merge field

Hi,

I have a merge field in my word document in the following way

{IF {MERGEFIELD} myCondition}="Yes"
"This text will show when condition is true" 
  {MERGEFIELD feedbackText}
"
"
"}

In my case feedbackText again has a text something like following

Your Product “is great”. Keep building

Now when I Execute and replace in C# the merge fields using Aspose.Words then value shows only upto Your Product and it gets cutoff at the first qoute. What is the solution for this ?

I have tried replacing it with double double qoutes and \" but nothing works. It just cuts off as it believes it to be end of if block.

@amaurya

Summary
The IF field stops at the first double‑quote inside the merged text because Word treats the quote as the end of the true‑text argument. In a normal Word field you would escape an inner quote by writing it as two consecutive quotes (""). However, the provided evidence only confirms the existence of the Field.GetFieldCode method in Aspose.Words and does not explicitly document the quoting rules for IF fields.

Why it happens
Word (and consequently Aspose.Words) parses an IF field using the syntax:

IF <expr1> <operator> <expr2> "<true‑text>" "<false‑text>"

The true‑text and false‑text are delimited by double‑quote characters. If the true‑text itself contains a double‑quote, the parser interprets the first unescaped quote as the closing delimiter, so everything after that quote is ignored. This is why the merged value Your Product "is great". Keep building is truncated at Your Product.

What to do next

  1. Escape quotes in the merge data – replace each " in the value that will be inserted into the IF field with two quotes (""). For example, the data should contain:
"Your Product ""is great"". Keep building"
  1. Make sure the IF field in the template is correctly quoted – the field code should have a single pair of outer quotes around the whole true‑text block, while any inner quotes are doubled:
{ IF { MERGEFIELD myCondition } = "Yes" "This text will show when condition is true { MERGEFIELD feedbackText }" "" }
  1. Perform the mail‑merge – after escaping the quotes in the data, execute the merge as usual and update fields:
doc.MailMerge.Execute(fieldNames, fieldValues);
doc.UpdateFields();
doc.Save("Result.docx");
  1. Verify the field code (optional) – you can inspect the field code after the merge using field.GetFieldCode() to ensure the doubled quotes are present.

After these steps the merged document should display:

This text will show when condition is true Your Product "is great". Keep building

Note: The above explanation follows the standard Word field syntax for escaping quotes, but the supplied evidence only confirms the existence of the Field.GetFieldCode method in Aspose.Words and does not directly document the quoting rules. Please test the solution in your environment and refer to the official Microsoft Word field code documentation for additional confirmation.

:warning: I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.

SDK version unclear — refer to the official release notes before applying this guidance.

Tried this already it doesn’t work.

@amaurya It is normally is not allowed to use double quotes in IF field values. And the only way to have a double quite in the IF field is using an embedded field like this { QUOTE 34 } . This is MS Word limitation. You can try using IFieldMergingCallback to resolve the problem.

Document doc = new Document("C:\\Temp\\in.docx");
doc.MailMerge.UnconditionalMergeFieldsAndRegions = true;
doc.MailMerge.FieldMergingCallback = new QuoteResolver();
doc.MailMerge.Execute(new String[] { "condition", "desc1", "desc2" },
        new String[] { "true", "The \"first\" description", "The \"second\" description" });
doc.Save("C:\\temp\\out.docx");
private class QuoteResolver : IFieldMergingCallback
{
    public void FieldMerging(FieldMergingArgs args)
    {
        string fieldValue = (string)args.FieldValue;
        if (!fieldValue.Contains("\""))
            return;

        String[] parts = fieldValue.Split('\"');
        DocumentBuilder builder = new DocumentBuilder(args.Document);
        builder.MoveToField(args.Field, false);
        for (int i = 0; i < parts.Length; i++)
        {
            builder.Write(parts[i]);
            if (i < parts.Length - 1)
                builder.InsertField("QUOTE 34");
        }
        args.Text = "";
    }

    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {
        // Do nothing.
    }
}

in.docx (14.1 KB)
out.docx (11.3 KB)

Hi alexey,

I used you solution but it still doesn,t work. Following are my input file and output generated document.
in.docx (15.1 KB)
out.pdf (34.2 KB)

Merge fields are replaced by following JSON

{
	"formData": {
		"ancillaryActivities": {
			"withinASeparateEntity": [
				{
					"ancillaryActivitiyDescription": "Lorem Ipsum is simply dummy(“Bob”) text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,(“Trust”) when an unknown printer took a galley of type and scrambled it to make a type specimen book. ",
					"isServiceDerivedFromLegalPractice": "Yes"
				}
			]
		}
	}
}

Note that if I put the merge field outside if condition it renders properly. But inside if condition it breaks on first qoute. Is there any other workaround for this ?

@amaurya In your data there are and , i.e. left and right double quotes. The code above process only regular (") double quote. Please try modifying code like this:

string fieldValue = (string)args.FieldValue;
fieldValue = fieldValue.Replace("“", "\"").Replace("”", "\"");
1 Like

Thanks @alexey.noskov this worked

1 Like