Double quotes in MergeFields

Hi.

As you know there is a problem with " in mergefields. Word will cut the text after them for example in IF fields.
One solution is to add a quote field instead.
{ QUOTE 34 } is the same as "

I asked myself why does not aspose words handle this for me? Perhaps with some option to disable/enable this behaviour or is there some way I can do this by myself via an attached event handler?

Kind regards,
TK

Hi Torsten,

Thanks for your inquiry. First of all please note that Aspose.Words is a class library designed for server-side processing of Microsoft Word documents and supports calculation of IF field. For more information, please visit the following link:
https://docs.aspose.com/words/java/update-field/

I would also suggest you please read the following article about mail merge in Aspose.Words:
https://docs.aspose.com/words/java/mail-merge-and-reporting/

Should you have any questions then, please don’t hesitate to ask. We are always glad to help you.

Best Regards,

Hi Torsten,

Thanks for your inquiry.

You have proposed an interesting work around to this problem. However I’m not fully sure how you would like Aspose.Words to take advantage in thie situation.

Would you like to see AW automatically “escape” nested speech marks like this inside field codes? If so, how would it detect which speech marks to escape?

Thanks,

Hi Adam,

it would be easier for me if AW would do it.
The quote character is only a problem in IF fields. I’m sure when you replace a mergefield with its value you already have a way to know if it is in a IF field or not (from the links above I read that you add the quotes if you need them). And then it is really simple. You have to replace every quote in the mergefield value with the quote field before you add it to the if field. quotes are not handled correcty by word in if fields it will cut off a lot. so they usually will destroy the meaning of it.
That also means AW have to handle quote fields correctly.

Doing it manually I think I would have to check all fields to find if fields and analyse their fieldcodes if they contain a quote character in the true and false value which is much more complicated as if you do it when you write the field.

Handling it by myself during mail merge with some event handlers could work too. But would be compicated again.
Allowing me to write mergefield values including new fields would work too so I could replace the quote characters with the quote field by myself. But that means you have to extend AW to allow this to me.

For example I had an issue yesterday.
I have the following fields
{ IF “«role.leadingteam.iscollective»” = “1” “” “«role.name»” }

After mailmerge it looked like this
IF “0” = “1” “” "“Der Wind”"

As you can see role.name was replaced with “Der Wind”. The customer has added the quotes in the Value and they destroyed the IF condition. It was printed nothing where it should print the name cause of the quotes.
For me handling this in the application is not really possible. It is Words fault to not be able to escape it in some way.
{ IF “0” = “1” “” “{QUOTE 34}Der Wind{QUOTE 34}” }
should work fine.

It feals natural for me that AW should handle this.

Kind regards
TK

Hi Torsten,

Thank you for the additional information.

First of all please note that Aspose.Words currently doesn’t support QUOTE fields. I have linked your request to the appropriate issue. You’ll be notified as soon as this new feature is available.

Secondly, yes you’re right, if the value of merge field, i.e. wrapped inside an IF field, contains double quotes then the whole IF field gets destroyed. I will discuss this potential issue with the concerned Aspose.Words developer and provide you more information.

If we can help you with anything else, please feel free to ask.

Best Regards,

Hi Torsten,

Thanks for this additional information.

We will see what the developer has to say about the internal implementation, but for the time being you can use the work around below. Actually it seems QUOTE field is supported in the model. The code below is a field merging handler which automatically replaces double quotes with a QUOTE field.

doc.MailMerge.FieldMergingCallback = new EscapeDoubleQuotesHandler();
doc.MailMerge.Execute(new string[]
{
    "MyField"
}, new object[]
{
    "\"Der Wind\""
});
public class EscapeDoubleQuotesHandler: IFieldMergingCallback
{
    public void FieldMerging(FieldMergingArgs args)
    {
        string fieldValue = args.FieldValue as string;
        if (fieldValue != null && fieldValue.Contains(speechmarkChar))
        {
            if (mBuilder == null)
                mBuilder = new DocumentBuilder(args.Document);
            // Manually insert the field result while replacing any speechmarks with quote fields.
            mBuilder.MoveTo(args.Field.Start);
            foreach(char c in fieldValue)
            {
                if (c == speechmarkChar)
                    mBuilder.InsertField("QUOTE 34");
                else
                    mBuilder.Write(c.ToString());
            }
            // We have already inserted the text so we can just remove the merge field.
            args.Text = string.Empty;
        }
    }
    public void ImageFieldMerging(ImageFieldMergingArgs args)
    {}
    private DocumentBuilder mBuilder;
    private
    const char speechmarkChar = '"';
}

Thanks,

Thank you very much for your help Adam.
I will try your code and let you know if I have any issues with it

Kind regards,
TK

The issues you have found earlier (filed as WORDSNET-5650) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(3)

Hi

Can you give me a hint how you solved the issue?

Kind regards
TK

Hi Torsten,

The fix was for full support and update of the QUOTE field which was implemented in Aspose.Words 11.1.0. You will still need to use the code above to achieve what you are looking for.

Thanks,

While it looks to be handled for documents created programmatically, what if the template-writer (the person preparing the template) needs to use double-quotes inside a field?

I have tried to put {QUOTE 34} inside my .docx template, and it does not process at all.

Please let me know. Thank you.

Hi Torsten,

Thanks for your inquiry. I have attached a couple of test documents here for your reference. In the input Word document, you’ll find that I have manually inserted two quote fields which after execution of the following code snippet instead of displaying speech characters, turn into a digit 34. I have logged this problem as WORDSNET-6991 in our issue tracking system. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.

Document doc = new Document(@"c:\temp\in.docx");
doc.MailMerge.Execute(new string[]
{
    "name"
}, new string[]
{
    "Der Wind"
});
doc.UpdateFields();
doc.Save(@"C:\temp\out.docx");

Best Regards,

The issues you have found earlier (filed as WORDSNET-6991) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.