'Insert Table Sub SetTable () set myrange = activeDocument.range (start: = 2, end: = 2) ActiveDocument.tables.add Range: = MyRange, Numrows: = 3, NumColumns: = 4END SUB
'Get Word General Strings Sub getText () set myrange = activeDocument.range (start: = 0, end: = 4) msgbox myrange.textend Sub
'Number of data in the WORD table Sub getTableCellText () DIM S for i = 1 to activeDocument.tables.count for irow = 1 to activeDocument.tables (i) .Rows.count for iCol = 1 to activeDocument.tables (i). Column.count set mycell = activedocument.tables (i) .cell (rotum = irow, column: = icol) s = s & mid (mycell.range.text, 1, len (mycell.range.text) - 2) Next Icol next irow next i msgbox send sub
Cell's properties RowIndex and Colindex come to get a row list in a table.
========================== Delphi ============================================================================================================================================================================ ============ WordApp: TWordApplication; WordDoc: TWordDocument; DocInx, oFileName, CfCversions, oReadOnly, AddToRctFiles, PswDocument, PswTemplate, oRevert, WPswDocument, WPswTemplate, oFormat: OleVariant; myRange: Range; myCell: Cell; Myrow: row; mycol: column;
IF not assigned (WordApp) THEN / / ===== Create object ===== begin Wordapp: = TWordApplication.create (nil); Wordapp.visible: = false; end; if not assigned (WordDoc) THEN Begin Worddoc : = TORDDocument.create (nil);
DocInx: = 1; oFileName: = InFile; oReadOnly: = true; CfCversions: = EmptyParam; AddToRctFiles: = EmptyParam; PswDocument: = EmptyParam; PswTemplate: = EmptyParam; oRevert: = EmptyParam; WPswDocument: = EmptyParam; WPswTemplate: = EmptyParam; oFormat: = EmptyParam; // ===== open file ===== WordApp.Documents.open (oFileName, CfCversions, oReadOnly, AddToRctFiles, PswDocument, PswTemplate, oRevert, WPswDocument, WPswTemplate, oFormat); WordDoc.ConnectTo ( Wordapp.documents.Item (Docinx)); / / ===== Associated file =====
// Take the character content of the entire text, including the table S: = WordDoc.range.text; // Take 1 - 4-bit characters, including the table myrange: = worddoc.range; myrange.start: = 0; myrange.end_ : = 4;
// Method (1) ==> Rule Table for i: = 1 to WordDoc.tables.count do begin for irow: = 1 to worddoc.tables.Item (i) .rows.count do // 第 第 表 表 r row BEGIN for ICOL: = 1 to WordDoc.Tables.Item (i) .Columns.count do // Part ICOL column Begin mycell: = worddoc.tables.Item (i) .Cell (iRow, ICOL); memo1.line. Add (mycell.range.text); end; end; end; // method (2) ==> irregular table: only horizontal merges for i: = 1 to worddoc.tables.count do // 第 表 图Begin for irow: = 1 to worddoc.tables.Item (i) .rows.count do beg myrow: = worddoc.tables.item (i) .Rows.Item (iRow); // 第 i 行 for iCol: = 1 To myrow.cells.count do // Part ICOL column Begin mycell: = myrow.cells.Item (ICOL); memo1.lines.add (mycell.range.text); end; end;
// Method (3) ==> Irregular: horizontal, longitudinal merged for i: = 1 to worddoc.tables.count do begin 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; // Merge first, second column iStart: = WordDoc.tables.Item (i) .Cell (1, 1) .range.start; mycol: = worddoc.tables.Item (i) .columns.Item (2); Iend: = mycol.cells.Item (Mycol) .Cells.count) .range.nd_; myrange: = worddoc.range; myrange.start: = iStart; myrange.end_: = id; myrange.cells.mege; 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. Wordapp.disconnect; WordApp.destroy; WordApp: = nil; end; / for i: = 1 to worddoc.tables.count do // i table begin for irow: = 1 to worddoc.tables.Item (i) .Rows.count do for iCol: = 1 to worddoc.tables.Item (i) .columns.count do mycell: = worddoc.tables.Item: = WordDoc.tables.Item (i) .Cell (IROW, ICOL); // Take [IROW, ICOL] column value end;