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

Free Support Forum - aspose.com

ExecuteWithRegions With Already Built Table

I have a Word document, and I know that it will have exactly 10 rows in a table, and I wish for the table to already be built in the Word document. So I do not wish Aspose to expand the table, but to fill in the already built table. Possible?

Derek

Hi

Thanks for your request. You can try to use DocumentBuilder class to achieve this. See the following code example.

Document doc = new Document(@"in_97825.doc");

DocumentBuilder builder = new DocumentBuilder(doc);

int rowCount = doc.FirstSection.Body.Tables[0].Rows.Count;

for (int i = 0; i < rowCount; i++)

{

int cellCount = doc.FirstSection.Body.Tables[0].Rows[i].Cells.Count;

for (int j = 0; j < cellCount; j++)

{

builder.MoveToCell(0, i, j, 0);

builder.Write(String.Format("this is {0} row {1} column", i.ToString(), j.ToString()));

}

}

doc.Save("out_97825.doc");

Also you can insert MergeField into the cells and then fill your table using MailMerge.

I hope that it will help you.

Best regards.

A couple questions:

Can you show this code in vb.net?

If I use the executewithregions on a table, I have obviously located the tablestart tableend section in the first row. Is there a way to reference THAT table in code. For example, if I have no data in my DataTable, I wish to remove the table in the Word doc entirely (which is just the first row).

Also, how do I find the correct table I am looking for (if there are more than one)?

All I have in the table is the first row with the merge fields (including tablestart and tableend).

Hi

Please, attach your template. I will investigate it and try to help you.

Best regards.

In my code, I will properly have 4 rows in a datatable in mytable, and 4 rows in mytable2. I know I can use executewithregions to do this, except I cannot have the table pre-built with executewithregions.

And I know I can loop with documentbuilder, but if there are no rows in a datatable (DataTable.Rows.Count=0), then I wish to remove the table entirely.

So how do I find my table, first of all. Do I need to use a bookmark around it, and somehow find the bookmark? I have to first find it to know it is the one to use documentbuilder to loop the records and fill in the 4 rows. But if DataTable.Rows.Count=0, then I want to delete that table, and not just have a table with a header row and 4 blank rows.

Make sense?

Also, would you use vb.net?

Hi

Thanks for the additional information. Try to use the following code.

Document doc = new Document(@"in1_97825.doc");

DocumentBuilder builder = new DocumentBuilder(doc);

//move to merge field

builder.MoveToMergeField("mytable");

if (builder.CurrentParagraph.ParentNode.NodeType == NodeType.Cell)

{

// get table

Table table = (builder.CurrentParagraph.ParentNode as Cell).ParentRow.ParentTable;

if(mytable.Rows.Count>0)

{

//fill table

}

else

{

table.Remove();

}

doc.Save("out1_97825.doc");

}

I hope that it will help you.

Best regards.

Can you do in vb.net?

I cannot translate this line into vb.net - the "as cell" part.

Table table = (builder.CurrentParagraph.ParentNode as Cell).ParentRow.ParentTable;

Hi

Here is code in VB.

Dim doc As Document = New Document("in1_97825.doc")

Dim builder As DocumentBuilder = New DocumentBuilder(doc)

'move to merge field

builder.MoveToMergeField("mytable")

If builder.CurrentParagraph.ParentNode.NodeType = NodeType.Cell Then

'get table

Dim table As Table = CType(builder.CurrentParagraph.ParentNode, Cell).ParentRow.ParentTable

If mytable.Rows.Count > 0 Then

'fill table

Else

table.Remove()

End If

End If

doc.Save("out1_97825.doc")

Best regards.

Can you create the code above into vb also... The code was:

Document doc = new Document(@"in_97825.doc");

DocumentBuilder builder = new DocumentBuilder(doc);

int rowCount = doc.FirstSection.Body.Tables[0].Rows.Count;

for (int i = 0; i < rowCount; i++)

{

int cellCount = doc.FirstSection.Body.Tables[0].Rows[i].Cells.Count;

for (int j = 0; j < cellCount; j++)

{

builder.MoveToCell(0, i, j, 0);

builder.Write(String.Format("this is {0} row {1} column", i.ToString(), j.ToString()));

}

}

doc.Save("out_97825.doc");

Hi

Here is code in Visual Basic.

Dim doc As Document = New Document("in.doc")

Dim builder As DocumentBuilder = New DocumentBuilder(doc)

Dim rowCount As Integer = doc.FirstSection.Body.Tables(0).Rows.Count

Dim i As Integer = 0

For i = 0 To rowCount - 1

Dim cellCount As Integer = doc.FirstSection.Body.Tables(0).Rows(i).Cells.Count

Dim j As Integer = 0

For j = 0 To cellCount - 1

builder.MoveToCell(0, i, j, 0)

builder.Write(String.Format("this is {0} row {1} column", i.ToString(), j.ToString()))

Next

Next


doc.Save("out.doc")

Best regards.