We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

MailMerge with custom object in table and html results in wrong position

Hello,

we start to evaluate Aspose.Words against TX TextControl because I already use some Aspose parts at the company Beta Systems and was very happy.
But now at the beginning we have noticed, that Html are inserted at wrong position in the table.
Also the number of parts is wrong - only one part is produced, but the are 3 parts in the enumeration.
We also would like to achieve simple html parts with same paragraph styling as in the template without code html styles (because the code and template may be changed by very different person).

I’ve created and attached the demo (without a huge Aspose.Words nuget package). After click at “DO IT!!!” tghe result will be written to bin\debug\TestTemplate…_out.docx. As you can see, the text is placed at the wrong position.

Any help will be appreciated!

Thanks,
AlexAsposeWordsTest.zip (101.6 KB)

@Alex1974

Thanks for your inquiry. You are using Aspose.Words without license. Please get the temporary license and apply it using License.SetLicense method.

Please do not use InsertHtmlWithBuilderFormatting method. We suggest you please use DocumentBuilder.InsertHtml method (String html, Boolean useBuilderFormatting). When useBuilderFormatting is true, formatting of inserted text is based on DocumentBuilder formatting, and the text looks as if it were inserted with DocumentBuilder.Write.

Regarding the content at wrong position in output document, please share the screenshots of problematic sections of output document and expected output Word document. We will then provide you more information about your query. Please use MS Word to create your expected output document.

Thanks for your cooperation.

Thank you for suggestions!

I will try it with the temporary licence and see if this makes a difference.

Regarding your suggestion, the first try was the InsertHtml (it can be seen in the attached code as outcommented line), but the was no difference in the output.
I will do some screenshoots and attach those here within a next hour.

Alex

Please excuse me, but I’ve not understand how exactly to obtain a temporary license, I only see the options to buy normal licences.

Hello Tahir,

now I have combined multiple steps (scratched from another forum thread topic!) and got a temporary licecnce. But this is very weird.

After licence is applied I got all three parts.

But the positioning problem is still exists. Template and code is in archive of first post.

I’ve made two creenshoots. The first one with blue rectangles is where merge fields are. The second one with red rectangles shows a misplaced output. Both made with MSWord. There is absolutelly no difference which one of InsertHtml is used and also which second parameter (true or false) is used on InsertHtml(p1, p2, p2)

Now, the first screenshoot:
image.png (8.1 KB)

And now the wrong result:
image.png (10.3 KB)

Followup:
That is much more weird. If I remove both upper tables, than text applies on top three times (!), which is a number of parts.

image.png (9.1 KB)

Next followup.
Now I think this is general problem with fields Builder.MoveToMergeField.
I’ve simply do mBuilder.Writeln(“TEST FELD”); e.Text="" instead of InsertHtml(); e.Text="" and the output is also appears at wrong position. So, the bug has primarly nothing with InsertHtml as well.

You can also test it with simplified template, attached here:
image.png (8.5 KB)
MainTestOhf4.zip (23.1 KB)

@Alex1974

Thanks for sharing the detail. You are using dot (.) in the mail merge fields. We suggest you please do not use it in the mail merge field names. Please read the known issue about mail merge from following article.
Known issue when using mail merge.

Thank you for a reply.
But in such case I’ll be unable to use master-details relations with generic MailMergeDataSource as shown in a entirely post archive code. Without that we will need to implement IMailMergeDataSource for nearly all (hundrets) of classes and maintain this over the next years. That would be a total overkill and blocks our work for next weeks. With TX TextControl we don’t need this at all - plain objects with master-details can be used as well.

Actually I evaluate an other way - serialize and read back as DataSet and use ExceuteWithRegions. This works with some drawbacks. One of them - I can’t imagine why IF-Fields are not working as expected - output is always the ELSE-Text.

So, within DataSet (because it does not work with generic interface provider) now I’ve tried all the options within IF-Field and don’t got it work :frowning:

Attached screenshoot and demo code.

image.png (3.2 KB)
WpfAsposeWords.zip (96.2 KB)

Any ideas?

@Alex1974

Thanks for your inquiry. In your case, you do not need to use mail merge with regions. We suggest you please read about mail merge and mail merge with regions from here:
About Mail Merge

It is not recommended to use dot (.) in mail merge field name. If you still want to use dot in mail merge field name, please return null value in GetChildDataSource method and set the field’s value in GetValue method. You can check the field name e.g. “Item.Name” in this method and set the fieldValue object as shown below. Hope this helps you.

public bool GetValue(string fieldName, out object fieldValue) {

    switch (fieldName)
    {
        case "Hund.Hundname":
            fieldValue = "Hund.Hundname value";
            return true;
        case "Ergebnis.UnbefangenheitIdKontrolle3AsHtml":
            fieldValue = "Ergebnis.UnbefangenheitIdKontrolle3AsHtml value";
            return true;
        default:
            fieldValue = null;
            return false;
    }
}

Thanks for your response and suggestions.

Because we have hundrets of properties and some object depth this can quickly results in a multiple hundrets of case blocks. For a single template. This is huge amount of work and not maintenable as well.

So, in entirely code I’ve tried to work aroundthis problem and return a property in GetValue over reflection. This works nearly perfect, because I saw, that a during of MergeField estimation, the single parts separated by a dots results in a separate calls to a GetValue. So I return an property value as in a example I found somewhere, but without call .ToString() at the end. And I was wondered about this, because I don’t need to do point-separation and recursive call itself.

My problem was: what if some properties returns an enumerable value? Because of this I have tried to implement GetChildDataSource as well.

But, this solution works only, wenn works correctly, only within one nesting depth :thinking:
And because I don’twant to implement an interface for thousands of objects, I need more generic solution. And this is, by the time, only a DataSet which I can get over a serialisation.

So, I don’t have a better idea for a generic solution.

Additionally, maybe I got a liitle bit stupid at late evening, but I can went out, why IF-Fields in nesting regions, as supplied in my example, are not evaluated at all.

Thank you very much for your effort and help!

@Alex1974

Thanks for your inquiry. You need to add the mail merge field with name “ID” in IF field. Please use the IF field in your document as shown below.

{ IF “{ MERGEFIELD ID }” = “3” “x” “” }

Thank you!
I did this also, but it does not work.
But… without doublequotas in first part, as in other examples here in forum.
Is the syntax changed in last versions?

@Alex1974

Thanks for your inquiry. We have modified the document for IF field and tested the scenario. It works as expected. We have attached the modified input document and output document here for your kind reference. Docs.zip (168.7 KB)

Please make sure that you are setting the value of mail merge field ID in GetValue method.

public bool GetValue(string fieldName, out object fieldValue)
{
    Console.WriteLine(fieldName);
   if (fieldName == "ID")
   {
       fieldValue = "3";
       return true;
   }
// your code....
}

Hello. Please excuse me, maybe I do something repeaditly wrong, but it is not worked as expected. Also not with suggested double quotas. I attached actually data and code as archive WpfAsposeWords_01032019_v1.zip again.

Used template is MainTestOhf4.docx.

IF-Field is now defined as { IF “{ MERGEFIELD Ruede }” = “abc” “OK” “NOK” \* MERGEFORMAT }
The ending \* MERGEFORMAT is always attached by MS Word itself.

Results is always NOK.

Code uses XML as DataSet from attached file MainTest.xml.
As we can see, the accordingly entry is defined as Root->Hundelist->Hund-Ruede abc for both entries.

So, why it is not evaluaed correctly? Other mergefield without evaluation produces correctly output.

Many thanks in advance!WpfAsposeWords_01032019_v1.zip (275.7 KB)

@Alex1974

Thanks for your inquiry. In your document, you added { MERGEFIELD Ruede } as text instead of mail merge field. Please check the attached image for detail.

You can test this case using attached simplified document. If field test.zip (9.6 KB)

Moreover, the relationship between tables should be correct. Please read the following article.
How to Set up Relations for use in Nested Mail Merge with Regions

Thank you very much!
Now I had understand, the text in if-field should not be typied in, but the merge-field should be inserted. Sadly, I don’t understand this from the documentation. Now it works! … but the if-field is not selectable/editable anymore, the menu-option “Edit” (“Bearbeiten” in german version) is grayed out. But this is problem of MSWord.

Now I’m found something different. If I try to delete the last empty page as suggested with code below, I had a difference on top of last page of last entry. I’ve attached screenshoot and code.

image.png (9.2 KB)

    // Cleanup code from https://forum.aspose.com/t/mail-merge-with-regions-creates-blank-page-at-the-end-of-document/164859/4
    
    while (string.IsNullOrEmpty(doc.LastSection.Body.LastParagraph.ToString(SaveFormat.Text).Trim()))
    {

        int childnodes = doc.LastSection.Body.LastParagraph.ChildNodes.Cast<Node>().Where(child => child.NodeType != NodeType.Run).ToList<Node>().Count;
        if (childnodes > 0)
            break;
        else
            doc.LastSection.Body.LastParagraph.Remove();
    }

The cleanup-options MailMergeCleanupOptions.RemoveEmptyParagraphs can’t be used also, because in such a case, all page-breaks between report parts would be deleted also, so next parts continues on the same page as previous part.

Any ideas?

Thanks!

@Alex1974

Thanks for your inquiry.

Yes, this is expected behavior of MS Word.

Please use the latest version of Aspose.Words for .NET 19.2, generate the output document, ZIP and attach it here for testing. We will investigate the issue and provide you more information on it.

Thank you for your assistance!
The latest version 19.2 is already in use.
I’ve attached an archive with both files: one without empty page removal (cleanup code is outcommented) and one with.ResultsWithAndWithoutRemoval.zip (45.2 KB)