Delphi and Word_VBA

'插入表格
Sub setTable()
  Set myRange = ActiveDocument.Range(Start:=2, End:=2)
  ActiveDocument.Tables.Add Range:=myRange, NumRows:=3, NumColumns:=4
End Sub
'取得Word常规字符串
Sub getText()
  Set myRange = ActiveDocument.Range(Start:=0, End:=4)
  MsgBox myRange.Text
End Sub
'取得Word 表格中的数据
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).Columns.Count
         Set myCell = ActiveDocument.Tables(i).Cell(Row:=iRow, Column:=icol)
         s = s & Mid(myCell.Range.Text, 1, Len(myCell.Range.Text) - 2)
       Next icol
     Next iRow
  Next i
  
  MsgBox s
End Sub
Cell's properties RowIndex and ColIndex to get the row and column number of a cell in the 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:= TWordDocument.Create(nil);
    end;
    DocInx:=1;
    oFileName := InFile;
    oReadOnly:=true;
    CfCversions := EmptyParam;
    AddToRctFiles:= EmptyParam;
    PswDocument:= EmptyParam;
    PswTemplate:= EmptyParam;
    oRevert:= EmptyParam;
    WPswDocument:= EmptyParam;
    WPswTemplate:= EmptyParam;
    oFormat:= EmptyParam;                                  // ===== 打开文件 =====
    WordApp.Documents.open(oFileName,CfCversions,oReadOnly,AddToRctFiles,PswDocument,
                           PswTemplate,oRevert,WPswDocument,WPswTemplate,oFormat);
    WordDoc.ConnectTo(WordApp.Documents.Item(DocInx)); // ===== Associated file =====
    //Get the character content of the entire text, including tables
    := WordDoc.Range.text;  
    // Take 1 -- 4 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 //i-th table iRow row
      begin
        For icol := 1 To WordDoc.Tables.Item(i).Columns .Count do // iCol column
        begin
          myCell:=WordDoc.Tables.Item(i).Cell(iRow,icol);
          memo1.Lines.add(myCell.Range.Text);
        end;
      end;
    end;
    
   
    //Method (2)==> Irregular table: only when horizontally merged
    For i := 1 To WordDoc.Tables.Count do //i-th table
    begin
      For iRow := 1 To WordDoc.Tables.Item(i) .Rows.Count do
 &
nbs Delphi and Word_VBA part 2:
p; begin
        myRow:=WordDoc.Tables.Item(i).Rows.Item(iRow); // iRow row
        For icol := 1 To myRow.Cells. Count do // iCol column
        begin
          myCell:= myRow.Cells.Item(iCol) ;
          memo1.Lines.add(myCell.Range.Text);
        end;
      end;
    end;
    //Method (3)==> Irregular : When merging horizontally and vertically
    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;
    
    //合并第一、二列
        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.End_;
        myRange:=WordDoc.Range;
        myRange.Start:=iStart;
        myRange.End_ :=iEnd;
        myRange.Cells.Merge;
    if Assigned(WordDoc) then                           // ===== 关闭文件 =====
    begin
      WordDoc.Close;
      WordDoc.Disconnect;
      WordDoc.Destroy;
      WordDoc := nil;
    end;
    if Assigned(WordApp) then                           // ===== 关闭Word =====
    begin
      WordApp.Quit;
      WordApp.Disconnect;
      WordApp.Destroy;
      WordApp := nil;
    end;
/////////////////////////////    
    For i := 1 To WordDoc.Tables.Count do   //第 i 个表
    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(i).Cell(iRow,iCol);    //取[iRow,iCol]列值    
    end;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324977667&siteId=291194637