E
E
Evgeny Ivanov2018-06-18 07:17:19
Delphi
Evgeny Ivanov, 2018-06-18 07:17:19

How to work with Word tables in Delphi?

I am using RAD Studio 10.1 Berlin.
There was such task:
There is a word the document in which a large number of tables.
It is necessary to view the document from beginning to end and, when meeting the table, read the data from 1 column 1 row. If it says "Table", then you
need to read the data of 2 columns, from 3 rows to the end of the table.
How can I do that?
I understand that the task is big - I will be glad to any code examples.

Working code example

procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
wdApp, wdDocs, wdDoc : Variant;
file_name:string;
temp_string:AnsiString;
begin

file_name:=ExtractFilePath( Application.ExeName )+'\test.doc';

//Пытаемся запустить MS Word и получить ссылку на объект Application.
//- Это корневой объект в объектной структуре MS Word.
try
wdApp := CreateOleObject('Word.Application');
except
ShowMessage('Не удалось запустить MS Word. Действие отменено.');
Exit;
end;
  
//Если требуется, делаем видимым окно MS Word.
wdApp.Visible := false;
//Ссылка на коллекцию документов.
wdDocs := wdApp.Documents;
//Попытка открыть выбранный файл и получить ссылку на объект документа.
wdDoc := wdDocs.Open(FileName:=file_name);

{	
CurTable:=ActiveDocument.Tables(1);
CurTable.Cell(10,20).Range.Text:=Text;
goto end_of_this;
}
  
//Читаем текст документа и записываем его в Мемо.
temp_string:=wdDoc.Range.Text;

//Закрываем MS Word.
wdApp.Quit;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kalapanga, 2018-06-18
@logpol32

What is the problem? Here is your example. The tables form the Tables collection. Number of tables - .Tables.Count. Write a cycle on the tables and read from them to your health what you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question