Operate tables in Word in Delphi

Source: Internet
Author: User

Procedure getwordcellstr;
VaR

Wordapp: twordapplication;
Worddoc: tworddocument;
Dockers, filename, cfcversions, isreadonly, addtorctfiles, pswdocument,
Pswtemplate, revert, wpswdocument, wpswtemplate, formatword: olevariant;
I, irow, icol: integer;
Mycell: cell;
Myrow: row;
Begin
Memo. lines. Clear;

// ===== Create an object ====
If not assigned (wordapp) then
Begin
Wordapp: = twordapplication. Create (NiL );
Wordapp. Visible: = false;
End;
If not assigned (worddoc) then
Worddoc: = tworddocument. Create (NiL );
Try
Docsag: = 1;
Filename: = 'd:/test.doc ′;
Isreadonly: = true;
Cfcversions: = emptyparam;
Addtorctfiles: = emptyparam;
Pswdocument: = emptyparam;
Pswtemplate: = emptyparam;
Revert: = emptyparam;
Wpswdocument: = emptyparam;
Wpswtemplate: = emptyparam;
Formatword: = emptyparam;
// ===== Open the file =====
Wordapp. Documents. Open (filename, cfcversions, isreadonly, addtorctfiles,
Pswdocument, pswtemplate, revert, wpswdocument, wpswtemplate, formatword );
// ===== Associated file ====
Worddoc. connectto (wordapp. Documents. Item (Dockers ));

// Method (1) ==> rule table
For I: = 1 to worddoc. Tables. Count do // table I
Begin // row irow
For irow: = 1 to worddoc. Tables. Item (I). Rows. Count do
Begin // column icol
For icol: = 1 to worddoc. Tables. Item (I). Columns. Count do
Begin
Mycell: = worddoc. Tables. Item (I). Cell (irow, icol );
Memo1.lines. Add (mycell. range. Text );
End;
End;
End;

// Method (2) ==> irregular table: only when merging horizontally
For I: = 1 to worddoc. Tables. Count do // table I
Begin
For irow: = 1 to worddoc. Tables. Item (I). Rows. Count do
Begin
Myrow: = worddoc. Tables. Item (I). Rows. Item (irow); // line irow
For icol: = 1 to myrow. cells. Count do // column icol
Begin
Mycell: = myrow. cells. Item (icol );
Memo1.lines. Add (mycell. range. Text );
End;
End;
End;

// Method (3) ==> irregular: any table
For I: = 1 to worddoc. Tables. Count do // table I
Begin // number J of cells
For J: = 1 to worddoc. Tables. Item (I). range. cells. Count do
Begin
Mycell: = worddoc. Tables. Item (I). range. cells. Item (j );
Memo1.lines. Add (mycell. range. Text );
End;
End;

Finally
If assigned (worddoc) Then // ==== close the file ====
Begin
Worddoc. close;
Worddoc. Disconnect;
Worddoc. Destroy;
Worddoc: = nil;
End;
If assigned (wordapp) Then // ==== close word ====
Begin
Wordapp. Quit;
Wordapp. Disconnect;
Wordapp. Destroy;
Wordapp: = nil;
End;
End;
End;
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.