zoukankan      html  css  js  c++  java
  • Delphi 与 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的属性RowIndex和ColIndex来取得某格单元格在表中的行列号
    ==========================  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                          // ===== 创建对象 =====
        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));     // ===== 关联文件 =====
        //取整个文本的字符内容,包含表格
        s := WordDoc.Range.text;  
        //取 1 -- 4 位的字符 ,包含表格 
        myRange:=WordDoc.Range;
        myRange.Start:=0;
        myRange.End_ :=4;
        //方法(1)==> 规则表
        For i := 1 To WordDoc.Tables.Count do
        begin
          For iRow := 1 To WordDoc.Tables.Item(i).Rows.Count do  //第 i 个表 iRow行
          begin
            For icol := 1 To WordDoc.Tables.Item(i).Columns.Count do //第 iCol列
            begin
              myCell:=WordDoc.Tables.Item(i).Cell(iRow,icol);
              memo1.Lines.add(myCell.Range.Text);
            end;
          end;
        end;
        
       
        //方法(2)==> 不规则表:只有横向合并时
        For i := 1 To WordDoc.Tables.Count do               //第 i 个表
        begin
          For iRow := 1 To WordDoc.Tables.Item(i).Rows.Count do
     &nbs
    Delphi 与 Word_VBA第2部分:
    p;    begin
            myRow:=WordDoc.Tables.Item(i).Rows.Item(iRow);  //第 iRow 行
            For icol := 1 To myRow.Cells.Count do           //第 iCol列
            begin
              myCell:= myRow.Cells.Item(iCol) ;
              memo1.Lines.add(myCell.Range.Text);
            end;
          end;
        end;
        //方法(3)==> 不规则:横向、纵向合并时
        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;

  • 相关阅读:
    __weak
    c++界面设计皮肤工具
    执行游戏时出现0xc000007b错误的解决方法
    2.4.1-Java语言基础(常量)
    句法模式识别(一)-串文法
    一步一步写算法(之hash表)
    LaTeX新人教程,30分钟从全然陌生到基本入门
    初次当面试官的经历和感触
    Android入门第八篇之GridView(九宫图)
    Android-Cannot merge new index 66195 into a non-jumbo instruction的解决的方法
  • 原文地址:https://www.cnblogs.com/westsoft/p/8965732.html
Copyright © 2011-2022 走看看