delphitextfieldform-fieldsword-2013ole-automation

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. 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.

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.

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).

Does anyone know, what I'm doing wrong and how it will work?

Thanks in advance,

Lea


Solution

  • To access quick parts and check boxes you need to access 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;
    

    P.S: Delphi Wrapper doesnt contains "ContentControls" property so you need to work with Range as 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);