Board index » delphi » Filling a Word table the hard way

Filling a Word table the hard way

Hello all,

I have an invoice template in Word97 which I am trying to fill. Each field on
the invoice is represented by a bookmark which i replace using the following
function:

function TfPrinterDialog.ReplaceBookmark(bkMark, NewText: string): boolean;
var
        Param1, Param2, MyRange: olevariant;
begin
  Result := true;
  Param2 := wdGoToBookmark;
  Param1 := bkMark;
  if wrdDoc.Bookmarks.Exists(Param1) then begin
    MyRange := wrdDoc.GoTo_(param2,EmptyParam,EmptyParam,param1);
    MyRange.Select;
    MyRange.Text := NewText;
  end
  else
    Result := false;
end;
This mostly works, but I have a table with 4 columns and 5 rows that I am
having trouble with. The four columns are Service, Units, Cost, Price and
they have the row number as a suffix (Service1, Units1 etc..). I've built
another function to simplify this:

function TfPrinterDialog.PutToken(sToken: string; i: Integer; Temp:
string):boolean;
var
  sTemp1: string;
begin
  Result := false;
  if i < 6 then begin
    if Temp <> '' then begin
      sTemp1 := sToken + inttostr(i);
      result := ReplaceBookmark(sTemp1,Temp);
    end;
  end;
end;

which I call as follows:
    for i := 1 to InvoiceDetailsQry.RecordCount do begin
      PutToken('Service',i,IDQAction.Value);
      PutToken('Units',i,IDQUnits.asstring);
      PutToken('Cost',i,IDQPrice.asstring);
      PutToken('Price',i,IDQItemCost.asstring);
      InvoiceDetailsQry.Next;
    end;

However after running this the first column, service, contains the value of
Price and none of the other bookmarks have been replaced! I've stepped
through and the variables seem to be passing correctly. I suspect Word may do
something funny with bookmarks in a table. If I stop it after service then
the service is correctly displayed in the table, so it is actually
overwriting the same field 4 times.

Is anyone familiar with this behavior?

thanks

Marc Pelletier
-----------------------------------------------------------------------------
Data Donkey - Geophysical Data Processing, Consulting and Software
Development
            Creator of the POWER TOOLBOX utilities for OASIS montaj
      contact: m...@datadonkey.com  tel (306) 931-6853 or www.datadonkey.com

 

Re:Filling a Word table the hard way


<<Marc Pelletier:
 MyRange :=
wrdDoc.GoTo_(param2,EmptyParam,EmptyParam,param1);

Quote

The wdGotoBookmark constant doesn't work properly. Use
if wrdDoc.Bookmarks.Exists(Param1) then  
wrdDoc.Bookmarks.Item(Param1).Range.Text := NewText;
instead.

--
Deborah Pate (TeamB)
http://delphi-jedi.org
Sorry, no email please.

Re:Filling a Word table the hard way


Deborah Pate (TeamB) wrote in <VA.000004c4.0038d...@cableinet.co.not-this-
bit.uk>:

Quote
>The wdGotoBookmark constant doesn't work properly. Use
>if wrdDoc.Bookmarks.Exists(Param1) then  
>wrdDoc.Bookmarks.Item(Param1).Range.Text := NewText;
>instead.

Thanks, Deborah,

That works great. There is a small problem remaining. For some reason this
function replaces the bookmark with the text everywhere but in the table. In
the table the bookmark remains and it's text has been changed to the desired
text. And this behavior is only in column 2 and up. It works ok in column 1.

This is fairly easy to work around. I just have to hide the bookmarks.

Thanks

Marc
--
-----------------------------------------------------------------------------
Data Donkey - Geophysical Data Processing, Consulting and Software
Development
            Creator of the POWER TOOLBOX utilities for OASIS montaj
      contact: m...@datadonkey.com  tel (306) 931-6853 or www.datadonkey.com

Other Threads