InvalidOperationException when Mail Merge a value with block quotes

I have a Mail Merge template where the merged values contains block quotes. When this field is in the condition of a Mail Merge IF statement I get an InvalidOperationException. I have attached a sample application in which this can be reproduced.

Hi there,

Thanks
for your inquiry.

I have modified the method ProcessItem. Please check highlighted code below. Hope this helps you. Please let us know if you have any more queries.

private void ProcessItem(string fieldCode, Field field, object fieldValue)
{
    if (fieldCode.ToLowerInvariant().Contains("html"))
    {
        String[] fieldcodes = field.GetFieldCode().Trim().Split(new Char[] { ' ' });
        if (fieldcodes[2] != "")
        {
            Builder.MoveToMergeField(fieldcodes[2]);
            Builder.InsertHtml(fieldValue.ToString());
        }
        // Node fieldNode = field.Remove();
        // Builder.MoveTo(fieldNode);
        // Builder.InsertHtml(fieldValue.ToString());
    }
}

This change works for this simple test application but when I put this into my production code I get the following exception.

InvalidOperationException: Cannot remove because there is no parent.
The stacktrace is as follows:
at Aspose.Words.Node.Remove()
at xfbd1009a0cbb9842.xfedf115fd9c03862.x93a5aef725ab1a13(x5e36356bc92c609b x0f7b23d1c393aed9)
at xfbd1009a0cbb9842.xfedf115fd9c03862.x1d125084aa46bebd(x5e36356bc92c609b x0f7b23d1c393aed9)
at xfbd1009a0cbb9842.xfedf115fd9c03862.x0f4b7f3b2ee62d4b(x5e36356bc92c609b x0f7b23d1c393aed9)
at xfbd1009a0cbb9842.xfedf115fd9c03862.x945dc04ac4e4f5ab(xb0b76bad05a97c8b xd3311d815ca25f02)
at xfbd1009a0cbb9842.xfedf115fd9c03862.x7b62f4210c900502(xcf417e2db4fe9ed3 xb1a1ccf3b9674a87, xcf417e2db4fe9ed3 x8f8ce337d74845ae)
at xe86f37adaccef1c3.xc5c3f438428cb03b.xdeeb682062ef79a5()
at xe86f37adaccef1c3.xc5c3f438428cb03b.xd5da23b762ce52a2(xa11a4c48b53f49a6 xef1769c4fe6ae4ca, Boolean xc9c7b90943167aed)
at Aspose.Words.Reporting.MailMerge.x18dfca7c5fd2402f(xa11a4c48b53f49a6 xef1769c4fe6ae4ca)
at ArcherTech.MailMerge.MailMergeDocument.Merge(EntityList reader) in c:\Development\Archer\v5.5.2\ArcherTech\ArcherTech.MailMerge\MailMergeDocument.cs:line 328v

The exception breaks on the Document.MailMerge.Execute() method.
So the code…
Builder.MoveToMergeField(field) is throwing an exception.
But this code does not throw the exception…

Node fieldNode = field.Remove();
Builder.MoveTo(node);

I have had no luck reproducing this exception in a simple test program so I am just hoping I have given you enough information here that you can either help me fix the problem or help me find a way to reproduce it in a simple test program.

Hi there,

Thanks
for your inquiry. Could you please try the following code snippet at your end and let us know if you still face problem?

If you still face problem, please share your document here for testing. Please debug your code and share for which field you are getting the exception. I will investigate the issue on my side and provide you more information.

private void ProcessItem(string fieldCode, Field field, object fieldValue)
{
    if (fieldCode.ToLowerInvariant().Contains("html"))
    {
        String[] fieldcodes = field.GetFieldCode().Trim().Split(new Char[] { ' ' });
        if (fieldcodes[2] != "")
        {
            Builder.MoveToMergeField(fieldcodes[2], true, false);
            Builder.InsertHtml(fieldValue.ToString());
        }
        field.Remove();
    }
}

With the suggested code changes I get a different exception…
NullReferenceException: Object reference is not set to an instance of an object.
The exception occurs on the same line of code…Document.MailMerge.Execute()
Here is the stack trace…

at x28925c9b27b37a46.x98739d759efb5fe7.get_x1be93eed8950d961()
at x28925c9b27b37a46.x98739d759efb5fe7.xeea9b43f0c912fdb(Node xda5bf54deb817e37)
at xfbd1009a0cbb9842.xdbc94e34c2e61de1.x9ed442c1833e090b(x5c928e5f0a98a22c x9af1c2b12bb7a91a)
at xfbd1009a0cbb9842.x2d380f6141585930.x2408a6db33935c93(Boolean x0bc1a2ec253ad36c)
at xfbd1009a0cbb9842.x64629b07e6a0cb07.get_xf9ad6fb78355fe13()
at xfbd1009a0cbb9842.x64629b07e6a0cb07.xd961adf06ad48c3f(Boolean x9fc40ce4428c62cc, Boolean x6ee567696ab44a53)
at xfbd1009a0cbb9842.x64629b07e6a0cb07.xe572abd1407c334c()
at xfbd1009a0cbb9842.xca592a476766b11a.xc7e8ef87526d9280()
at xfbd1009a0cbb9842.xca592a476766b11a.xb333e1e6c01c2be2()
at xfbd1009a0cbb9842.xfedf115fd9c03862.xc0425e9d15f16649(Field xe01ae93d9fe5a880)
at xfbd1009a0cbb9842.xfedf115fd9c03862.x652f6183bc2805f1(Field xe01ae93d9fe5a880)
at xfbd1009a0cbb9842.xfedf115fd9c03862.x0f4b7f3b2ee62d4b(x5e36356bc92c609b x0f7b23d1c393aed9)
at xfbd1009a0cbb9842.xfedf115fd9c03862.x945dc04ac4e4f5ab(xb0b76bad05a97c8b xd3311d815ca25f02)
at xfbd1009a0cbb9842.xfedf115fd9c03862.x7b62f4210c900502(xcf417e2db4fe9ed3 xb1a1ccf3b9674a87, xcf417e2db4fe9ed3 x8f8ce337d74845ae)
at xe86f37adaccef1c3.xc5c3f438428cb03b.xdeeb682062ef79a5()
at xe86f37adaccef1c3.xc5c3f438428cb03b.xd5da23b762ce52a2(xa11a4c48b53f49a6 xef1769c4fe6ae4ca, Boolean xc9c7b90943167aed)
at Aspose.Words.Reporting.MailMerge.x18dfca7c5fd2402f(xa11a4c48b53f49a6 xef1769c4fe6ae4ca)
at ArcherTech.MailMerge.MailMergeDocument.Merge(EntityList reader) in c:\Development\Archer\v5.5.2\ArcherTech\ArcherTech.MailMerge\MailMergeDocument.cs:line 336v

The exception is occurring because of the IF merge field (last MERGE FIELD in the document)
{ IF "{ MEREGFIELD Definitions \*MERGEFORMAT } = " " "{ MERGEFIELD Definitions \!format "html:Definitions" \* MERGEFORMAT " }" "" }
I have attached the mail merge template. The “!format html:Definitions” is the special keyword we use to indicate this field needs to be formatted as HTML, which then goes through our process to do the Builder.InsertHTML. It seems to be the IF that is causing the problem. If I remove the IF the exception does not occur.
I would also like to mention that this code does that throw either exception (but this causes a problem with the quotes as stated at the top of thread)…

Node fieldNode = field.Remove();
Builder.MoveTo(fieldNode);
Builder.InsertHtml(fieldValue.ToString())

Again, I apologize that I am not able to reproduce this in a simple test. Hopefully this will be enough information to help.

Hi there,

Thanks
for your inquiry.

*Jeeno:

The exception is occurring because of the IF merge field (last MERGE FIELD in the document)

{ IF "{ MEREGFIELD Definitions *MERGEFORMAT } = " " “{ MERGEFIELD Definitions !format “html:Definitions” * MERGEFORMAT " }” “” }

I have attached the mail merge template. The “!format html:Definitions” is the special keyword we use to indicate this field needs to be formatted as HTML, which then goes through our process to do the Builder.InsertHTML. It seems to be the IF that is causing the problem. If I remove the IF the exception does not occur.*

In your case, I suggest you please replace mail merge fields (which contains special keywords like “!format html:Definitions”) as MailMergeField_HTML e.g Definitions_HTML. Please let us know if you still face problem.

*Jeeno:

I would also like to mention that this code does that throw either exception (but this causes a problem with the quotes as stated at the top of thread)…

Node fieldNode = field.Remove();
Builder.MoveTo(fieldNode);
Builder.InsertHtml(fieldValue.ToString())*

If you do not want to replace your mail merge fields as MailMergeField_HTML, please share your output document here which shows the undesired output here for our reference. We will then provide you more information about your query.

I am a bit confused…are you saying that using MailmergeField_HTML is a special keyword that Aspose will pick up and automatically insert the content as HTML, or is this something we would still have to capture and do the Builder.InsertHtml() to get the content to render in the Word document as HTML? If it is the latter than this will not resolve our problem…it is not the !format that is causing the problem, it is this code…
Builder.MoveToMergeField(…);
Builder.InsertHtml(…)

The exception does not occur when using this code…
Node fieldNode = field.Remove();
Builder.MoveTo(…);
Builder.InsertHtml(…)

but the exception does occur using this code…
Builder.MoveToMergeField();
Builder.InsertHtml()

In any case we will not be able to change the structure of the MERGEFIELD in which we identify a field that needs to be inserted as HTML because our customers already have hundreds of documents that are using the !format “html:Definition:”.
As far as your last statement of providing my output document, there is no output document because an exception occurs before the output can be rendered.

Hi there,

Thanks
for your inquiry. Please use the same code which you shared in the first post of this thread. See the ProcessItem method below.

The MailmergeField_HTML is not a special keyword. Please replace your mail merge fields which have format like (!format "html) with FieldName + HTML as shown below.

E.g Replace { MERGEFIELD Definitions !format "html:Definitions" * MERGEFORMAT } with { MERGEFIELD **Definitions_html** * MERGEFORMAT }

Hope this helps you.

private void ProcessItem(string fieldCode, Field field, object fieldValue)
{
    if (fieldCode.ToLowerInvariant().Contains("html"))
    {
        Node fieldNode = field.Remove();
        Builder.MoveTo(fieldNode);
        Builder.InsertHtml(fieldValue.ToString());
    }
}

If I use the code in my first post than I am back to the problem where I get an InvalidOperationException when block quotes are included in the field value.
As stated in my last post, we cannot change to MailMergeField_HTML because we have customers already using the existing syntax of !format “html”. We cannot ask them to change all their existing templates to use the new format. Again, this format is not causing the problem anyways.
It seems that no matter what code I use I get some exception
I get an InvalidOperationException when I use the code below and the MERGEFIELD is inside of an IF field

Builder.MoveToMergeField(field);
Builder.InsertHtml(fieldValue.ToString());

I get a NullReferenceException when I use the code below and the MERGEFIELD is inside of an IF field

Builder.MoveToMergeField(fieldcodes[2], true, false);
Builder.InsertHtml(fieldValue.ToString());
field.Remove();

I get another exception when I use the following code and the fieldValue contains block quotes…

Node fieldNode = field.Remove();
Builder.MoveTo(fieldNode);
Builder.InsertHtml(fieldValue.ToString());

So, it seems at this point that no matter which code I use some exception will occur at some point. At the very minimum please tell me that you will send this on to the developers so they can fix the issues in future versions. For now I guess I will concede there is no resolution to my initial post and I will find my own workaround.

Hi there,

Thanks
for your inquiry. Unfortunately, I am unable to reproduce this issue using the shared application. It would be great if you please debug your code and share the field value for which this exception is thrown.

It is difficult to say what the problem is without the simplified application. We need the simplified application with the specific field value which causes this issue. I appreciate your cooperation in advance for this.

Which issue are you unable to reproduce? You can at least reproduce the issue in the initial post, correct?

Hi there,

Thanks
for your inquiry. Please accept my apologies for this confusion. I was using the modified code share in my first post here. Regarding the issue shared in your first post, I have logged this problem in our issue tracking system as WORDSNET-10228. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

I have some additional information regarding my initial post that might help your developers. It seems that it is not only the block quotes, but regular quotes that causes the issue. I have attached another sample application that demonstrates this. I replace the block quotes with regular quotes and get the same exception. I replace the block quotes with spaces and the exception does not occur. Hope this helps.

Hi there,

Thanks
for sharing the detail. I have logged this detail for WORDSNET-10228. We will update you via this forum thread once this issue is resolved. Thanks for your patience.

Hi there,

Thanks for your patience.

It
is to update you that our development team has completed the
analysis of this issue and has come to a conclusion that this issue has
been closed with ‘‘Won’t Fix’’ resolution. I am quoting the developer’s comments here:

*The problem occurs because customer tries to create field like this

IF “Test” <> “NotTest” “Nest text in quotes “Hello”” “False”.

The expected result should be ( Nest text in quotes “Hello” ).

If you try to put the same string into the IF field using MS Word it will change IF field behavior because quotes are special symbols which MS Word uses to construct IF field conditions.

In MS Word to insert quotes into the field you have to use { QUOTE 34 } field. Please see the following link for details: https://answers.microsoft.com/en-us/msoffice/forum/all/double-quotes-inside-of-the-word-field/62b660c6-a55e-4782-ad09-65599be54b29*

I get that but Aspose does not give me the ability to change the quotes to { QUOTE 34 }. I have created a separate issue on this.
<a href="https://forum.aspose.com/t/54875

Also, I am disappointed that the developer has decided it is acceptable to throw an exception and kill the process when this situation occurs, especially with such a vague exception that tells us nothing of why the exception occurs. I would hope you could come up with a better mechanism to report the problem to the consumer.

Hi there,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does. If you try to put the same string into the IF field using MS Word it will change IF field behavior because quotes are special symbols which MS Word uses to construct IF field conditions.

I have replied your query here about replacing quotes in html with { QUOTE 34 }. Please follow that thread for further proceedings.