简体   繁体   English

如何在Word中查找表格并使用Delphi更新表格中的值

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

I am new to Ole word automation using Delphi. 我是使用Delphi进行Ole单词自动化的新手。 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. 但是我无法找到特定的表并使用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. 从表面上看,您应该能够使用MS Word的Selection对象的Goto方法导航到给定的表。 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. 但是,存在一个问题(请参见此答案的结尾),以检测由于Goto找不到正确的表而导致操作何时失败。

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': 如果感兴趣的表在文档中带有可识别的文本标签,则可以简单地搜索该标签,如果找到该标签,则可以从中向前导航,例如本示例,该示例在标签“ 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. 如所写,“ if Found ...”块仅将光标置于表的第一个单元格的第一个字符上。 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. 如果要查找如何在表格单元格中插入图像之类的方法,请转到Word功能区上的“开发人员”选项卡,记录一个可以执行所需操作的宏,然后使用“ 宏”弹出窗口中的“ 编辑 ”来查看它-通常很容易,然后将其剪切并粘贴到Delphi中,然后将其编辑为等效的Delphi代码。 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: 要在文档中找到第N个表格并将光标放在其左上角的单元格中,可以执行以下操作:

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. 顺便说一句,在Word的“查找”对话框的“ Goto选项卡上,“ Go to what列表框中有“ Table条目。 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. 与查找返回WordBool查找不同,Goto返回一个Range对象。 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. 如果尝试使用它转到仅包含2个表的文档中的第10个表,则不会引发错误,但是返回的范围是文档中的最后一个表。 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. 我还没有找到一种方法来从返回的Range中检查Goto是否成功,而不检查与表相关联的某些文本,而这些文本最初可能是使用Find找到的。 Of course, if the document is guaranteed to contain the table you're looking for, this problem with Goto probably needn't concern you. 当然,如果保证文档包含您要查找的表,则Goto的此问题可能与您无关。

Maybe something like: 也许像:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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