Keep CHPX of table when adding rows

Questions and answers on how to use DOCXReadWrite
Post Reply
Gasper
Posts: 6
Joined: Wed Feb 07, 2024 7:53 am

Keep CHPX of table when adding rows

Post by Gasper »

I'm adding rows to a TAXWTable and I want those rows to keep the same formatting (font, font size, color, etc.) as the first row.

Code: Select all

//ATable: TAXWTable, TableRow: TAXWTableRow
TableRow := ATable.Rows.Add;
TableRow.Assign(ATable[0], False); //Font of first cell of first row is 'Times New Roman' (Also tried AAssignText True)
	
TableRow.Cells[0].Paras.PlainText := 'Sample Text'; //Font of cell is 'Arial'
I have found half of the solution

Code: Select all

chpx := TAXWCHPX.Create(nil);
chpx.FontName := 'Times New Roman';
	
TableRow.Cells[0].Paras.FormatAll(chpx); 
But this hard sets the font to Times New Roman and our clients want to be able to customize their docx templates with their desired fonts, sizes and colors and not have them changed when filling the table with data.

How do I retain the formatting of the table from the template?
Vinzenz
Posts: 4
Joined: Mon Feb 20, 2023 1:24 pm

Re: Keep CHPX of table when adding rows

Post by Vinzenz »

Hi,
you can try something like

Code: Select all

chpx.FontName := ATable.FirstCell.Paras.Last.CHPXAtPos(0).FontName;
I don't know if it still works without text in the first cell, but it should copy whatever font is used there.
As long as you can access any TAXWLogPara the option to get a FontName is there, so you can experiment a bit.
Gasper
Posts: 6
Joined: Wed Feb 07, 2024 7:53 am

Re: Keep CHPX of table when adding rows

Post by Gasper »

This did the trick! Thank you!!
Post Reply