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

Free Support Forum - aspose.com

Insert Cells at MergeFiled Location

I have recently upgraded from an older version of Aspose.Words and have run into a change in behavior that I am having a problem resolving.
I have a word document with a merge filed located in a table. I wish to insert a new row at that location. Previously using InsertCell would create a new row in the table at the location of the merge filed, but now it creates a new table in the cell where the merge field is located.
The intention is that sometimes when there is no data, I do not want a blank row, so the merge field is located in the first cell of the next row where there is fixed data.
Here is an example of what the Word document contains:

neighborhood
Street Name Frontage Direction Lanes Parking
«streets»Lighting «lighting»
Intersection Controls «intersection»
Adjacent Uses
North of Subject «nSubject»
South of Subject «sSubject»
East of Subject «eSubject»
West of Subject «wSubject»
Notable Landmarks «landmarks»
Neighborhood Boundaries
North «nBound»
South «sBound»
East «eBound»
West «wBound»
Freeways Name Miles Direction
«freeways»

So in this example if I have data to be inserted where the “streets” merge field is located, I wish to have a new row there with the following row starting with “Lighting”. The other merge fields are filled using the MailMerge.Execute method (except for “freeways” where the same methodology as used for “streets” is to be employed).
What is the procedure for achieving this effect with the current version of Aspose.Words?

Hi
Thanks for your request. Could you please attach your template document and expected output document? I will review these documents and provide you a solution. Also it would be useful to get your code.
Best regards.

Hi Alexey.
I’ve attached the requested files. In generate.cs the method generateWord is where the template document is processed. We have several template documents depending on the type of report that the user is attempting to generate. The snippet from my original post is located at about halfway through the document and has “Neighborhood” as the table header.
Thanks for your help.
Lee

Hi
Thank you for additional information. I think that you should just append newly created row to existing table. Please see the following code snippet.

builder.MoveToMergeField("streets");
builder.ParagraphFormat.Alignment = ParagraphAlignment.Left;
builder.Italic = true;
// Get Row where document Buidlet cursor is placed
Row streetRow = builder.CurrentParagraph.GetAncestor(NodeType.Row) as Row;
if (street1Name.Length > 0)
{
    builder.InsertCell();
    CreateLeftBorder(builder);
    builder.CellFormat.Width = 110.65;
    builder.Write(street1Name);
    builder.InsertCell();
    builder.CellFormat.Borders.ClearFormatting();
    builder.CellFormat.Width = 83.05;
    builder.Write(street1Frontage);
    builder.InsertCell();
    builder.CellFormat.Borders.ClearFormatting();
    builder.CellFormat.Width = 83.05;
    builder.Write(street1Dir);
    builder.InsertCell();
    builder.CellFormat.Borders.ClearFormatting();
    builder.CellFormat.Width = 83.05;
    builder.Write(street1Lanes);
    builder.InsertCell();
    CreateRightBorder(builder);
    builder.CellFormat.Width = 83;
    builder.Write(street1Parking);
    Row newRow = builder.EndRow(); //END ROW HERE
                                    // Insert created row before streetRow
    streetRow.ParentTable.InsertBefore(newRow, streetRow);
}

This is your code. I highlighted my changes.
Hope this helps.
Best regards.

Alexey:
That worked perfectly. Thank you for your quick response and resolution.
Lee