The main problem in this code is re-adding existing items. You take an existing item (using GetItem method) and add it in another place (using AddItemAsIs or AddItem).
You cannot do it. Items are owned by the document. If an item is inserted several times, when the document is freed, the item will be freed more than once, and the application crashes.
To make it work correctly, you need to create copies of items instead.
There is an undocumented method AppendFrom, it does this work. It creates copies of items of the source document and adds them to the destination document:
Code: Select all
/// / populate cells with Vars and text FROM THE FIRST ROW
for r := 1 to table.RowCount - 1 do
for c := 0 to table.ColCount - 1 do
table.Cells[r, c].AppendFrom(table.Cells[0, c]);
If all your documents are like the sample one, you can use this solution.
Most item types can be duplicated, but not all of them.
The most universal method for copying documents is using TMemoryStream:
Code: Select all
var
Stream: TMemoryStream;
DummyColor: TColor;
/// / populate cells with Vars and text FROM THE FIRST ROW
Stream := TMemoryStream.Create;
for c := 0 to table.ColCount - 1 do
begin
Stream.Clear;
table.Cells[0, c].SaveRVFToStream(Stream, False, clNone, nil, nil);
for r := 1 to table.RowCount - 1 do
begin
Stream.Position := 0;
table.Cells[r, c].LoadRVFFromStream(Stream, DummyColor, nil, nil, nil, nil)
end;
end;
Stream.Free;