0

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

Lea
  • 261
  • 5
  • 14

1 Answers1

1

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);
Lea
  • 261
  • 5
  • 14
Agustin Seifert
  • 1,938
  • 1
  • 16
  • 29
  • I tried it. it worked for the formfields but not for my textfield. Instead of getting the textfield's content I got the content of my quick part again. And can you explain to me what these content controls are? I have to build a bigger program and need to get as much information as possible for this. :) – Lea Sep 07 '15 at 15:12
  • You access textfield through "Shapes" property – Agustin Seifert Sep 07 '15 at 15:33
  • ok that works for the textfield. but I still get 2 times the content of the quickpart. this is my result: First line [#13#10] Second line [#13#10] Third line [#13#10] [#13#10] Fifth line – fourth line was empty [#13#10] [#13#10] Company xy [#13#10] [#13#10] Autor [#13#10] Name [#13#10] [#13#10] Company xy [#13#10] [#13#10] This is a textfield – Lea Sep 08 '15 at 06:28
  • I debugged it....The quick part and the formfields are already read by the Memo1.Lines.Add(WordDocument1.Paragraphs.Item(i).Range.Text); the Memo1.Lines.Add(LRange.ContentControls.Item(i).Range.Text); does only read the quick part and the Memo1.Lines.Add(WordApplication1.ActiveDocument.Shapes.Item(i).TextFrame.TextRange.Text); does read the text field. – Lea Sep 08 '15 at 06:37
  • If my answer helps you, please consider accept it. Thanks! – Agustin Seifert Sep 08 '15 at 14:35
  • then please correct your answer. i googled a bit. the first code is not for textfields but for check boxes and things like that. and could you please take a look at my next [question](http://stackoverflow.com/questions/32456461/how-to-read-text-from-a-header-and-footer-in-word-2013-docx-with-delphi-xe7) – Lea Sep 08 '15 at 14:45