简体   繁体   English

如何使用Delphi XE7从Word 2013的text-和formfield中获取文本?

[英]How do I get the text from text- and formfields in Word 2013 with Delphi XE7?

I built a small program to read the all the text from a .docx with Delphi. 我构建了一个小程序,使用Delphi从.docx中读取所有文本。 It works with normal text and with quick parts but none of the solutions I found in forums and tutorials works for my textfields or form fields. 它适用于普通文本和快速部分,但我在论坛和教程中找到的解决方案均不适用于我的文本字段或表单字段。 I'm using Word 2013 and Delphi XE7 and my document has 2 form fields, one named "Name", the other "Author", and one textfield. 我正在使用Word 2013和Delphi XE7,我的文档有2个表单字段,一个名为“名称”,另一个为“作者”,以及一个文本字段。

Here's my code: 这是我的代码:

    procedure TForm1.Button1Click(Sender: TObject);
    var i: integer;
    begin
      WordApplication1.Disconnect;
      WordDocument1.Disconnect;
      try
        WordApplication1.Connect;
        WordApplication1.Visible := true;
        WordDocument1.ConnectTo(WordApplication1.Documents.Open(
          'C:\homelaufwerk\Documents\Embarcadero\Studio\Projekte\Word test\testDoc.docx',
          EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam,
          EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam));
        memo1.Clear;
        memo1.Text := '';

        for I := 1 to WordDocument1.Paragraphs.Count do
        begin
          memo1.Text := memo1.Text + WordDocument1.Paragraphs.Item(i).Range.Text + #13#10;
        end;

        memo1.Text := memo1.Text + WordApplication1.ActiveDocument.FormFields.Item('Author').Result;
        memo1.Text := memo1.Text + WordApplication1.ActiveDocument.FormFields.Item('Name').Result;

        for I := 1 to WordApplication1.ActiveDocument.Fields.Count do
        begin
          memo1.Text := memo1.Text + WordApplication1.ActiveDocument.Fields.Item(1).Result;
        end;

        WordDocument1.Close;

      except
        ShowMessage('Microsoft Word couldn''t start.');
      end;
    end;

I also tried this for the textfield: 我也在文本字段中尝试过此操作:

    WordDocument1.Fields.Item(1).Select;
    memo1.Text := memo1.Text + WordApplication1.Selection.Text;

and at every place I replaced WordApplication1.ActiveDocument.[...] with WordDocument1.[...] and the other way round...nothing works. 然后我在每个地方都用WordDocument1。[...]替换了WordApplication1.ActiveDocument。[...],反之……则无济于事。

When I'm debugging, the .Count function for the textfield always returns 0 and when trying to get the .Result for the form fields I get the error that the fields could not be found (don't know the error's wording in english). 当我调试时,文本字段的.Count函数始终返回0,而尝试获取表单字段的.Result时,我得到找不到该字段的错误(不知道该错误的英语措辞) 。

Does anyone know, what I'm doing wrong and how it will work? 有谁知道,我在做错什么,以及它将如何工作?

Thanks in advance, 提前致谢,

Lea

To access quick parts and check boxes you need to access ContentControls. 要访问快速零件和复选框,您需要访问ContentControls。

var
  ... // your other vars here
  LRange: OleVariant;
  LStartRange: Integer;
  LEndRange: Integer;
begin
  ...
  ... //here your code
  ...

  LStartRange := WordApplication1.ActiveDocument.Content.Start;
  LEndRange := WordApplication1.ActiveDocument.Content.End_;
  LRange := WordApplication1.ActiveDocument.Range(LStartRange, LEndRange);

  for I := 1 to LRange.ContentControls.Count do
    Memo1.Lines.Add(LRange.ContentControls.Item[I].Range.Text);

end;

PS: Delphi Wrapper doesnt contains "ContentControls" property so you need to work with Range as OleVariant PS:Delphi包装器不包含“ ContentControls”属性,因此您需要使用Range作为OleVariant

Update: 更新:

To access textfield items: 要访问文本字段项:

for I := 1 to WordApplication1.ActiveDocument.Shapes.Count do
  Memo1.Lines.Add(WordApplication1.ActiveDocument.Shapes.Item(I).TextFrame.TextRange.Text);

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

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