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

Free Support Forum - aspose.com

Clone a Row and Insert Row in Existing Table in Word Document using C++ Code

I’m opening a document and trying to add a row to an existing table. Here is the code I’m using:

System::SharedPtr<Table> table = System::DynamicCast<Table>( doc->GetChild( NodeType::Table, 0, true ) );
System::SharedPtr<DocumentBuilder> builder = System::MakeObject<DocumentBuilder>(doc);
builder->MoveToCell( 0, 0, 0, 0 );
builder->EndRow();
builder->InsertCell();

It errors on EndRow with “Cannot end a row while not building a table.”

If I add builder->StartTable() before MoveToCell, it errors on MoveToCell with “rowIndex”.

If I add StartTable after MoveToCell, it errors on EndRow with “Cannot end a row in this state.”

@danieltobey,

Please ZIP and attach the following resources here for testing:

  • Your simplified input Word document
  • Your expected DOCX file showing the desired output. You can create this document by using MS Word.

As soon as you get these pieces of information ready, we will start investigation into your scenario and provide you code to achieve the same by using Aspose.Words for C++. Thanks for your cooperation.

Hi @awais.hafeez,

Thanks for looking into this. Here’s the zip file with the sample documents and sample code. Let me know if you need anything else.
danieltobey Aspose Table Output.zip (17.9 KB)
danieltobey Aspose Table Output.zip (17.9 KB)

@danieltobey,

You can meet this requirement by cloning any row in Table of Word documnet. Please try the following Aspose.Words for C++ API’s code:

#include <Aspose.Words.Cpp/Licensing/License.h>
#include <Aspose.Words.Cpp/Model/Document/Document.h>
#include <Aspose.Words.Cpp/Model/Tables/Table.h>
#include <Aspose.Words.Cpp/Model/Tables/Row.h>

using namespace Aspose::Words;
using namespace Aspose::Words::Tables;

int main()
{
	System::SharedPtr<License> license = System::MakeObject<License>();
	license->SetLicense(u"Aspose.Total.Product.Family.lic");

	System::SharedPtr<Document> doc = System::MakeObject<Document>(u"My Doc In.docx");

	System::SharedPtr<Table> table = System::DynamicCast<Table>(doc->GetChild(NodeType::Table, 0, true));
	System::SharedPtr<Row> clonedRow = System::DynamicCast<Row>((System::StaticCast<Node>(table->get_LastRow()))->Clone(true));

	table->AppendChild(clonedRow);

	doc->Save(u"awcpp-20.1.docx");
}  

Hope, this helps.

1 Like