Board index » delphi » insert page break after tables in word

insert page break after tables in word

Hello all,

I've posted this question before, but did not get an answer.
But the problem still exists, so..... I give it another try.

I want to insert a serie of tables in the middle of an existing
word-document and want a pagebreak after each table.
I try to do this by replacing a bookmark. See the code below:
But when I do it this way I will first see all pagebreaks and after them all
tables without pagebreaks.
I have tried all kinds of other solutions but nothing seems to do the job.

Can you give me a hint how to solve this?

Regards,
Jan Janson

function Make_Merge_Table_Model_I4(iWord: WordApplication; iDocument:
WordDocument; cBookmark: string): boolean;
var
  nRow,nRows: Integer;
  iRange: Range;
  iTable: array of Table;
  nTable: Byte;
  ovBM: OleVariant;
  ovBreakKind: OleVariant;
  ovDirection: OleVariant;
begin
  with dmElection do begin
    ovBM := cBookmark;

    {select range of bookmark}
    iRange := iDocument.Bookmarks.Item(ovBm).Range;
    iRange.Select;

    nTable := 0;
    tblFPart.First;
    while (not tblFPart.eof) do begin
      Inc(nTable);
      SetLength(iTable,nTable);

      nRows := tblFPartCANDIDATES.Value;

      {add table}
      iTable[nTable-1] := iWord.ActiveDocument.Tables.Add(iRange,nRows,3);

      {fill table}
      nRow := 0;
      tblFKand.Locate('PARTY',tblFPartPARTY.Value,[]);
      while (not tblFKand.eof) and (tblFKandPARTY.Value=tblFPartPARTY.Value)
do begin
        Inc(nRow);
        iTable[nTable-1].Cell(nRow,1).Range.Text := tblFKandCAND_NO.Value;
        iTable[nTable-1].Cell(nRow,2).Range.Text := tblFKandNAME.Value;
        iTable[nTable-1].Cell(nRow,3).Range.Text := tblFKandCITY.Value;
        tblFKand.Next;
      end;

      {Collapse to end}
      ovDirection := wdCollapseEnd;
      iRange.Collapse(ovDirection);

      {insert pagebreak}
      ovBreakKind := wdPageBreak;
      iRange.InsertBreak(ovBreakKind);

      tblFPart.next;
    end;
  end;
end;

 

Re:insert page break after tables in word


<<Jan Janson:
I want to insert a serie of tables in the middle of an
existing word-document and want a pagebreak after each
table. I try to do this by replacing a bookmark. See the
code below:

Quote

A simplified version seems to work fine:

var
  R: Range;
  i: integer;
  ovBM: OleVariant;
  ovBreakKind: OleVariant;
  ovDirection: OleVariant;
..
  ovDirection := wdCollapseEnd;
  for i := 1 to 3 do
  begin
    ovBM := i;
    R := Doc.Bookmarks.Item(ovBm).Range;
    Doc.Tables.AddOld(R, 2, 3);

    {insert pagebreak}
    R.Collapse(ovDirection);
    ovBreakKind := wdPageBreak;
    R.InsertBreak(ovBreakKind);
  end;

Perhaps you need to check your bookmarks?

--
Deborah Pate (TeamB) http://delphi-jedi.org

  Use Borland servers; TeamB don't see posts via ISPs
  http://www.borland.com/newsgroups/genl_faqs.html

Re:insert page break after tables in word


Hi Deborah,

Thanks for your reply, but unfortunitly it did not solve my problem.

Your simplified version is somewhat different because I only use ONE
bookmark.
The reason for this is that the number of tables is variable.

So the code looks like this:

var
  R: Range;
  i: integer;
  ovBM: OleVariant;
  ovBreakKind: OleVariant;
  ovDirection: OleVariant;
..
  ovDirection := wdCollapseEnd;
  ovBM := 'List_Of_tables';
  R := Doc.Bookmarks.Item(ovBm).Range;

  for i := 1 to 3 do
  begin
   Doc.Tables.AddOld(R, 2, 3);

    {insert pagebreak}
    R.Collapse(ovDirection);
    ovBreakKind := wdPageBreak;
    R.InsertBreak(ovBreakKind);
  end;

and this generates a serie of page breaks BEFORE the serie of tables.

Do you have any idea?

Regards,
Jan Janson

Deborah Pate (TeamB) <d.p...@cableinet.co.not-this-bit.uk> schreef in
berichtnieuws VA.00000f65.0016a...@cableinet.co.not-this-bit.uk...

Quote
> <<Jan Janson:
> I want to insert a serie of tables in the middle of an
> existing word-document and want a pagebreak after each
> table. I try to do this by replacing a bookmark. See the
> code below:

> A simplified version seems to work fine:

> var
>   R: Range;
>   i: integer;
>   ovBM: OleVariant;
>   ovBreakKind: OleVariant;
>   ovDirection: OleVariant;
> ..
>   ovDirection := wdCollapseEnd;
>   for i := 1 to 3 do
>   begin
>     ovBM := i;
>     R := Doc.Bookmarks.Item(ovBm).Range;
>     Doc.Tables.AddOld(R, 2, 3);

>     {insert pagebreak}
>     R.Collapse(ovDirection);
>     ovBreakKind := wdPageBreak;
>     R.InsertBreak(ovBreakKind);
>   end;

> Perhaps you need to check your bookmarks?

> --
> Deborah Pate (TeamB) http://delphi-jedi.org

>   Use Borland servers; TeamB don't see posts via ISPs
>   http://www.borland.com/newsgroups/genl_faqs.html

Re:insert page break after tables in word


<<Jan Janson:
Your simplified version is somewhat different because I
only use ONE bookmark.

Quote

Ah. :)

The problem is that the Tables.Add method doesn't update
your range variable, which stays just where it was. All
your code does is add page breaks before the bookmark, and
tables after the bookmark - hence your problem. Try this:

var
  Tbl: Table;
..
  R := Doc.Bookmarks.Item(ovBm).Range;
  for i := 1 to 3 do
  begin
    Tbl := Doc.Tables.AddOld(R, 2, 3);
    R := Tbl.Range;

    {insert pagebreak}
    R.Collapse(ovDirection);
    ovBreakKind := wdPageBreak;
    R.InsertBreak(ovBreakKind);
  end;

--
Deborah Pate (TeamB) http://delphi-jedi.org

  Use Borland servers; TeamB don't see posts via ISPs
  http://www.borland.com/newsgroups/genl_faqs.html

Re:insert page break after tables in word


Deborah,

That's it!

It works fine now.
Thanks for your help.

Regards,
Jan Janson

Quote
> The problem is that the Tables.Add method doesn't update
> your range variable, which stays just where it was. All
> your code does is add page breaks before the bookmark, and
> tables after the bookmark - hence your problem. Try this:

Re:insert page break after tables in word


Quote
"Jan Janson" <Jan.Jan...@inter.NL.net> wrote in message <news:3bc3fe1a$1_2@dnews>...
> Hello all,

> I've posted this question before, but did not get an answer.
> But the problem still exists, so..... I give it another try.

> I want to insert a serie of tables in the middle of an existing
> word-document and want a pagebreak after each table.
> I try to do this by replacing a bookmark. See the code below:
> But when I do it this way I will first see all pagebreaks and after them all
> tables without pagebreaks.
> I have tried all kinds of other solutions but nothing seems to do the job.

> Can you give me a hint how to solve this?

Try to make this report with EK RTF report component for Delphi
http://gpb.skiftel.ru/ec/

It has construction Scan-endscan with option "page", to insert a page
break on each iteration of the cycle (besides the last)

Another way - insert a page break as #12 through the report variable.

Eugene Kuchugurov.

Other Threads