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

Free Support Forum - aspose.com

Suggestion for the proper use of Table.InsertAfter?

Hello,

I have a Word document that contains a table. My goal is to copy the the table (with Table.Clone) and to insert another copy of the table right after the current table. I’m trying to use Table.InsertAfter() but I’m not getting it quite right.

Would you be so kind as to suggest how best to accomplish this?
Thank you!


My current code is:

// Find the field reference
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToMergeField(“TableGroup”);
int numberOfCopies = 2; // An arbitrary value

if (builder.CurrentParagraph.IsInCell)
{
Aspose.Words.Tables.Cell cell = (Aspose.Words.Tables.Cell)builder.CurrentParagraph.ParentNode;
Aspose.Words.Tables.Table table = cell.ParentRow.ParentTable;

for (int k = 0; k < numberOfCopies; k++)
{
Aspose.Words.Tables.Table newTable = (Aspose.Words.Tables.Table)table.Clone(true);
table.InsertAfter(newTable, builder.CurrentParagraph);
}
}


I’m making a little progress by using:

table.ParentNode.InsertAfter(newTable, table);

By using this, at least I’m able to duplicate the table… now need to figure out how to get paragraph break between the Cloned tables and I’ll be set.

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. I think the following code could be useful for you.

//Open document

Document doc = new Document(@"Test068\in.doc");

//Get tale form document

Table tab = doc.FirstSection.Body.Tables[0];

//Clone table

Node clone = tab.Clone(true);

//Create new Paragraph

Paragraph par = new Paragraph(doc);

//Insert thsi paragraph after original table

tab.ParentNode.InsertAfter(par, tab);

//Insert clone after paragraph

par.ParentNode.InsertAfter(clone, par);

//Save output document

doc.Save(@"Test068\out.doc");

hope this helps.

Best regards.

That’s very helpful. Many thanks.

Mike

I have a follow-up question for you, Alexey.

After cloning the table, I want to locate a Merge Field within the cloned table (before it has been inserted into the document). The trouble is that using DocumentBuilder.MoveToMergeField never finds the field.

I have confirmed that the cloned table contains a merge field called
{ MERGEFIELD TableStart:CustomerData }

I’m trying to use:

Aspose.Words.Tables.Cell currentCell = newTable.FirstRow.FirstCell;
builder.MoveToMergeField(“TableStart:CustomerData”);
I was hoping that I could move DocumentBuilder to the first cell and then attempt to use MoveToMergeField. Any suggestions on how to locate a merge field in this newly cloned table?

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. Why do you need to move DocumentBuilder cursor to the TableStart merge field? If you need to fill region with data you should just execute mail merge with regions. If you have few regions with same name inside your document then you should just execute mail merge with regions several times.

I think this is the better approach.

Best regards.

Hello,

The reason is because, in a dynamic fashion, I am filtering the data that is used in the merge process and I want to merge only the filtered data into a copy of the table, thus achieving a “merge with categories”.

So, for example, say my merge template has a typical table set up with TableStart, etc. Then, let’s say I have a dataset that contains several data tables. My goal is to split one of the data tables into, say, four smaller data tables (which are added to the dataset).

If I were able to duplicate the target table in Word and update the TableStart fields to match the names of the newly created smaller data tables, I would ultimately perform a Merge with Regions once and the entire template would be populated (and the data would appear in various categories).

Perhaps what you’re suggesting is that I clone the table, append it to the Document, filter the data and then perform a merge with regions. Then repeat the entire process… (clone, filter the data, and perform another merge with regions). is that right? In that way, it may not be necessary to update the TableStart fields. I’ll ponder that…

Still, is the original question even possible?

Thanks
Mike

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your explanation. If I understood your requirements correctly you need move to TableStart mergefied to rename it. You can achieve this without DocumentBuilder. See the following code:

//Open document

Document doc = new Document(@"Test068\in.doc");

DocumentBuilder builder = new DocumentBuilder(doc);

//Get tale form document

Table tab = doc.FirstSection.Body.Tables[0];

//Clone table

Node clone = tab.Clone(true);

//Create new Paragraph

Paragraph par = new Paragraph(doc);

//Insert thsi paragraph after original table

tab.ParentNode.InsertAfter(par, tab);

//Insert clone after paragraph

par.ParentNode.InsertAfter(clone, par);

//Now we whould like to rename TableStart and Table end mergefields

// Select all field start nodes so we can find the merge fields.

NodeCollection fieldStarts = (clone as CompositeNode).GetChildNodes(NodeType.FieldStart, true, false);

foreach (FieldStart fieldStart in fieldStarts)

{

if (fieldStart.FieldType.Equals(FieldType.FieldMergeField))

{

MergeField mergeField = new MergeField(fieldStart);

if(mergeField.Name == "TableStart:CustomerData")

mergeField.Name = "TableStart:CustomerDataRenamed";

if(mergeField.Name == "TableEnd:CustomerData")

mergeField.Name = "TableEnd:CustomerDataRenamed";

}

}

//Save output document

doc.Save(@"Test068\out.doc");

The implementation of MergeFieldClass you can find here:

http://www.aspose.com/documentation/file-format-components/aspose.words-for-.net-and-java/rename-merge-fields.html

of course you can move DocumentBuilder cursor to the merge field. But before moving you should insert cloned table into the document. In this case there will be few mergfield with same name and first DocumentBuilder cursor will be moved to the first occurrence.

You can use the following line of code to move to mergefield with name “TableStart:CustomerData”

builder.MoveToMergeField("CustomerData");

Best regards.

That’s fantastic… many thanks!