简体   繁体   中英

How to find a table in word and update values in the table using delphi

I am new to Ole word automation using Delphi. I have a sample document which have many tables inside it. I am able to insert an image by finding a shape in word and insert values inside it. But I am not able to find a particular table and update some values into it using delphi. Is there a way? Thanks !

在此处输入图片说明

I assume you're asking mainly how to find the table, rather than how to change the contents of the table afterwards. How to do this depends on the criteria you want to use to find the table of interest.

On the face of it, you should be able to navigate to a given table using the Goto method of MS Word's Selection object. However, there is a problem with that (see at the end of this answer) in detecting when the operation has failed because Goto didn't locate the correct table.

If the table of interest is preceded in the document by an identifying text label, you could simply search for the label and, if found, navigate forwards from that, like this example which finds the table after the label 'Table3':

procedure TForm1.Button4Click(Sender: TObject);
var
  AFileName : String;
  MSWord,
  Document : OleVariant;
  Found : WordBool;
begin
  AFileName := 'd:\aaad7\officeauto\Tables.Docx';

  MSWord := CreateOleObject('Word.Application');
  MSWord.Visible := True;
  Document := MSWord.Documents.Open(AFileName);

  MSWord.Selection.Find.Text :='Table3';
  Found := MSWord.Selection.Find.Execute;
  if Found then begin
    MSWord.Selection.MoveDown( Unit:=wdLine, Count:=1);
  end;
end;

As written, the "if Found ..." block merely places the cursor on the first character of the first cell of the table. Once in the table, you can alter its contents however you like.

If you want to find out how to do something like insert an image in a table cell, go to the Developer tab on Word's ribbon, record a macro that does what you want and then use Edit from the Macros pop-up to look at it - it's usually fairly easy then to cut'n paste it into Delphi and edit it into the equivalent Delphi code. Same goes for other methods of finding the table you want - record a macro then translate it.

To find the Nth table in a document and plant the cursor in its top left cell, you can do this:

procedure TForm1.Button2Click(Sender: TObject);
var
  AFileName : String;
  MSWord,
  Document,
  Tables,
  Table : OleVariant;
  TableNo : Integer;
begin
  AFileName := 'd:\aaad7\officeauto\Tables.Docx';

  MSWord := CreateOleObject('Word.Application');
  MSWord.Visible := True;
  Document := MSWord.Documents.Open(AFileName);

  TableNo := 3;

  Tables := Document.Tables;

  if TableNo <= Tables.Count then begin
    Table := Tables.Item(TableNo);
    Table.Select;
    MSWord.Selection.MoveLeft( Unit:=wdCharacter, Count:=1);
  end;

end;

Btw, in Word's Find dialog, on the Goto tab, there is Table entry in the Go to what listbox. You can call that in code using something like

MSWord.Selection.GoTo(What:= wdGoToTable, Which:=wdGoToFirst, Count:=3);  

The problem with it is how to check in code whether it succeeded. Unlike Find, which returns a WordBool , Goto returns a Range object. If you try to use it to go to the 10th table in a document which only contains 2 tables, there is no error raised, but the returned range is the last table in the document. I haven't yet found a way to check from the returned Range whether Goto succeeded without checking some text associated with the table which could have been found using Find in the first place. Of course, if the document is guaranteed to contain the table you're looking for, this problem with Goto probably needn't concern you.

Maybe something like:

Word.ActiveDocument.Tables.Item( 1 ).Cell( 1, 1 ).Range.Text := 'some text';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM