Problems merging in a table

Hi,
I’ve some trouble filling rows in a word table using VB.NET.

I’ve tried to pass a data table to the builder:

d.MailMerge.ExecuteWithRegions(tab) (d is the aspose.wprd.document)

the template document table:

| TableStart:1«&269» | «&455» | «&267» | «&268» | «&270» | «&271»TableEnd:1 |
| Tot. | «&105» |Cbm. «&102» | Data Trasporto | «&098» |
| «&452» |

The data table:

&269 &455 &267 &268 &270 &271
40BX 1 . LAMIERE ALLUMINIO 1,00
40BX 1 . LAMIERE ALLUMINIO 1,00

The output document:

| Marcatura | Tipo | Colli | Imballo | Descrizione merce | Peso Lordo Kg. |
| | 40BX | 1 | . | LAMIERE ALLUMINIO | 1,00 |
| | 40BX | 1 | . | LAMIERE ALLUMINIO | 1,00 |
| | Tot. | | Cbm. | Data Trasporto| |

as you can see the last row (outside the tablestart / tableend) is not filled with data.

I can get this row filled if I put an additional row with empty fields.

The rest of the document is filled correctly.

Can anyone tell me where I`m wrong ?

tnx

I can send a well formatted word document if necessary.

Walimay

Hey,

Hm. First, I can’t see what data you wnat to be inserted to the merge fields outside of the merge region. Where is it? Second, if you want to populate such fields, you obviously need to run Execute in addition to ExecuteMergeRegion.

Or did you expect some other behaviour? Please clarify this point.

Hi,
tnx for your quick reply.

Here attached you will find a zip with the template, the result and the code.(data are in the previous post)

The problem is that if i put an empty row (as the attached dataset image) in the data table all the fields (before and after the table) in the doc are filled correctly, but i don’t want an empty row at the end of data in my table.

I don’t have any documentation about Aspose Words dll. where can I find it?

tnx. Waly

The following is still unclear to me: with what data you suppose to fill the fields outside of the table?

Here are the links to the Aspose.Words documentation:

API reference

Programmers Guide

Pay special attention to the Perform Mail Merge section of the programmers guide and the following topic:

https://docs.aspose.com/words/net/mail-merge-and-reporting/

For some reason I can’t see anything attached. Please reattach the zip.

Oh, and surely take a look to the demo projects supplied with the component. You can take ideas of performing mail merge from most of them.

well, the other part of the document is filled by data in two array:

_arrayvartestata the name of the fields to be replaced
_arraytestata the corresponding values.

pls notre that with the dataset in the attached zip all works fine, but i have 2 details row correctly filled plus an empty row. Just as the datset rows are.

I want to obtain the same result without the empty row.

I try to re-attach the zip.

tnx. Waly

Thanks. Unfortunately, I’m still unable to reproduce your situation. Maybe it would be great if you create a small project with a data source included which mail merges your document. But at least now I can make some notes regarding the code:

’ mi muovo sull’header

b.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary)

d.MailMerge.Execute(_arrayvartestata, _arraytestata)

No need to move to the header using the document builder because it does not affect mail merge.

For Each tab As DataTable In _dataset.Tables

d.MailMerge.ExecuteWithRegions(tab)

Next

No need to loop through the dataset tables because ExecuteWithRegions provides a special overload accepting the dataset.

I also see that you perform the same merge twice :

d.MailMerge.Execute(_arrayvartestata, _arraytestata) 

in InserisciTestata and in ReplaceMerge

then you do another merge

d.MailMerge.Execute(\_arrayvariabili, _arrayvalori)

and only then you do mail merge with regions:

d.MailMerge.ExecuteWithRegions(tab)

Sadly, I cannot understand the purpose of such the order because your comments in code are not in English. But are you sure everything is correct there? Please double check it and try to save and inspect the document after each merge. Probably you will catch the moment when the merge fields outside of the table get removed.

Post the results of your investigation here. If no luck, please compose a tiny project which executes your code and involves your data source. I need to reproduce the flow of the process to spot the problem.

many thanks for your patience.

I’ll double check the process and I’ll be back with the results.

Thanks for now.

Waly

pls sorry for the comment in italian language, next time i’ll translate 'em

Hi,

I did some changes and tests.

Unfortunately I’m not the original coder of this project, so I can’t understand the reasons of some subs and functions.

We are almost fine, only this part of code is necessary to obtain a correct document:

doc.MailMerge.Execute(_arrayvartestata, _arraytestata) 'inserts Header data (above the table)
doc.MailMerge.Execute(_arrayvariabili, _arrayvalori) 'insert body data (below the table)
doc.MailMerge.ExecuteWithRegions(_dataset) ’ insert table data

obviously _arrvartestata and _arraytestata are arrays containing variables names and values for the header, just as _arrayvariabili and _arrayvalori are for the body.

The dataset / ExecuteWithRegions works fine. Shall I do something different in case of more than one table ? The dataset will have many tables (a couple or three, maybe) each one with its own data. Does ExecuteWithRegions mange it or some additional code is necessary?

The laest questions are:

  1. can I put two (or more) variables in a single table cell ? (such as date and time variables in the same cell but with two variables / merge fields , like | <<&123>> / <<&456>> | )

  2. can a dataset row split on two table rows ?

thanks

  1. No, you shouldn’t do anything else to process a dataset except of simply passing it to ExecuteWithRegions. It will loop through all the tables consecutively and merge them all to the corresponding merge regions.
  2. Regarding two values in one cell: no, it’s impossible by using the default merge technique but you can consider customization of the merge process by means of mail merge handler. See here for more info. The main idea is that you catch the moment when a data record is encountered and then read and insert values as you need inside an event handler.
  3. Regarding splitting a dataset row - to be honest, I didn’t fully understand what and, more important, why you would require that. If you make it clear, maybe I’ll be able to suggest something else.

So has the initial problem disappeared? Feel free to post your further questions if you have any.

DmitryV:

Regarding splitting a dataset row - to be honest, I didn’t fully understand what and, more important, why you would require that. If you make it clear, maybe I’ll be able to suggest something else.

So has the initial problem disappeared? Feel free to post your further questions if you have any.

regarding the quoted question:

Maybe that data are on the same dataset row, but have to be splitted on two different table rows due to page with limitations.
Have I to put tablestart / tableend on each table row or at start / end of “logical” row ?

The initial problem was fully solved. Now i’m trying the other (two fields on same cell) but I beleave it’s almost solved too.

I’ll confirm you soon.

tnx. Walimay

Hi Dimitri.

GOOD JOB ! All works fine now, and I’ve understand some more about aspose.words.

I hope no need to bore you again with my questions.

You were very kind, quick and clear.

have a nice day. (and weekend !)

Kindly. Walimay

Hi Dimitri,
i’m back again.

the latest test was not perfect. I’ll try to explain:

passing a datatable with 18 columns to fill a template in a word table all is fine if I have only 1 row (dataset row)

if I have 2 or more dataset rows this is the result:

IMLU8912345 1x 20BX S.T.C. 40 400,000 0,400
IMLU8912345 1x 20BX S.T.C. 40 400,000 0,400
S1: S2:
S1: S2:
MARCHE SU PENNE PENNE
MARCHE SU PENNE PENNE
H. S. Code: 9965236591
H. S. Code: 9965236591

should be like this:

IMLU8912345 1x 20BX S.T.C. 40 400,000 0,400
S1: S2:
MARCHE SU PENNE PENNE
H. S. Code: 9965236591
IMLU8912345 1x 20BX S.T.C. 40 400,000 0,400
S1: S2:
MARCHE SU PENNE PENNE
H. S. Code: 9965236591

this is the template:

«TableStart:1»«&925» 1x «&928» S.T.C. «&931» «&932» «&934» «&935»«TableEnd:1»
S1: «TableStart:1»«&926» S2:«&927»«TableEnd:1»
«TableStart:1»«&559» «&933»«TableEnd:1»
«TableStart:1»«&334» «&335»«TableEnd:1»

obviuosly the dataset contains the right variables names and data. all in the same table.

how can I solve?

The code that fill the table: doc.MailMerge.ExecuteWithRegions(_dataset)

thanks. Walimay

Oh… I think I now understand why you would need “splitting” a datatable row. So you want the data from the same row to go to several table rows in your template? It would be easy to implement if your template only consisted of this table…

Apparently the way you’ve prepared the template is not applicable. Would you please attach it to let me see what could be done? And, for the future, I think that attaching documents is much more efficient than posting the contents right in the post

Yeah, you’re right… I saw the layout only after the post button click

here attached you will find the template. I’ll try to send you also some data to fill the table.

In this template I’ll have only one table. Maybe in other I have a couple.

Thanks for your help.

Walimay

Okay, if I unserstand you correctly, you want the whole table to be repeated so that one data row would correspond to the whole table. So what if you place the TableStart/TableEnd markings before and after the table? I.e.

TableStart:TableName

Table

TableEnd:TableName

Just watch the markings are outside of table rows. No more markings are needed.

Try it, does the result suit you?

Xtra cool.

I think it shoul suit my needs. I’ll try to test with more than one table.

I’ve already tried this, but putting Start / end inside the table…

You solved me a lot of probs.

many many tnx.

Walimay.