文档内容:
                  统计文档
工作内容总结:
1.xxxxxxxxxx
2.xxxxxxxxxxxx
3.xxxxxxxxxx用vb新建一个文档,用word.document对象.
新建了,但是不知道怎样输入内容,以及控制内容的格式,如统计文档为黑体,16号字,居中,下面的内容用12号,宋体.还要怎样改变文件名(不用保存)

解决方案 »

  1.   

    在Word中新录宏,录制后,在VB编辑器中查看代码。
      

  2.   

    Option   Explicit   
      '===============================   
      '生成电子表格文档模块   
      '===============================   
      Sub   ProcCrtDoc(rst   As   Recordset,   strDocTp   As   String)   
      '==============================   
      '同时生成简表和详表   
      '参数rst中必须包含的字段为:系统、编号、简要描述、优先级   
      '                                             作   者、创建时间、简要描述、测试目的、   
      '                                             预置条件、测试步骤、预期行为、备注   
      '不同的word版本其所支持的word对象方法有所不同   
      '===============================   
      Dim   n   As   Long   
      Dim   wdapp   As   Word.Application   
      Dim   wddoc   As   Word.Document   
      Dim   rngrange   As   Word.Range   
      Dim   sysID   As   Integer   
      Dim   sysName   As   String   
      Dim   bkstr()   As   String   
      Dim   rstSort   As   Recordset   
        
      If   rst.RecordCount   =   0   Then   
            MsgBox   "未选中任何记录!",   ,   "提示:"   
      Else   
        
      '将记录集按系统、编号排序   
      rst.Sort   =   "系统ID,用例编号"   
      Set   rstSort   =   rst.OpenRecordset   
        
      '定义书签数组大小   
      ReDim   bkstr(rstSort.RecordCount   +   1)   
        
      '建立word应用程序对象   
      Set   wdapp   =   New   Word.Application   
      wdapp.Visible   =   True   
        
      '新建一个文档   
      Set   wddoc   =   wdapp.Documents.Add   
        
      '初始一个不可能的系统ID   
      sysID   =   -1   
      n   =   0   
      '每个系统建立一个简表,并以系统名作为表的标题   
      Do   While   Not   rstSort.EOF   
            n   =   n   +   1   
            If   sysID   <>   rstSort.Fields("系统ID")   Then   
                sysID   =   rstSort.Fields("系统ID")   
                sysName   =   FunSysIDToName(sysID)   
      '定义标题   
                Set   rngrange   =   wddoc.Paragraphs(wddoc.Paragraphs.Count).Range   
                With   rngrange   
                      .Font.Bold   =   True   
                      .Font.Size   =   14   
                      .Font.Name   =   "隶书"   
                      .Text   =   sysName   
                      .Paragraphs.Alignment   =   wdAlignParagraphLeft   
                      .InsertParagraphBefore   
                      .Collapse   wdCollapseEnd   
                  End   With   
        
      '添加表头   
                  wddoc.Paragraphs.Add   
                  Set   rngrange   =   wddoc.Paragraphs(wddoc.Paragraphs.Count).Range   
                  With   rngrange   
                        .Tables.Add   rngrange,   1,   7   
                        .Tables(.Tables.Count).Style   =   "网格型"   
                        .Tables(.Tables.Count).Columns(1).SetWidth   35,   wdAdjustProportional   
                        .Tables(.Tables.Count).Columns(2).SetWidth   35,   wdAdjustProportional   
                        .Tables(.Tables.Count).Columns(3).SetWidth   200,   wdAdjustProportional   
                        .Collapse   wdCollapseEnd   
                  End   With   
                  With   wddoc.Tables(wddoc.Tables.Count).Rows.Last   
                        .Range.Font.Bold   =   True   
                        .Range.Font.Name   =   "宋体"   
                        .Range.Font.Size   =   10   
                        .Cells(1).Range.Text   =   "序号"   
                        .Cells(2).Range.Text   =   "编号"   
                        .Cells(3).Range.Text   =   "简要描述"   
                        .Cells(4).Range.Text   =   "优先级"   
                        .Cells(5).Range.Text   =   "测试结果"   
                        .Cells(6).Range.Text   =   "测试时间"   
                        .Cells(7).Range.Text   =   "备注"   
                        .Range.Collapse   wdCollapseEnd   
                  End   With   
      

  3.   

    '添加一行数据   
                  wddoc.Tables(wddoc.Tables.Count).Rows.Add   
                  With   wddoc.Tables(wddoc.Tables.Count).Rows.Last   
                        .Range.Font.Bold   =   False   
                        .Range.Font.Name   =   "宋体"   
                        .Range.Font.Size   =   10   
                        .Cells(1).Range.Text   =   n   
                        .Cells(2).Range.Text   =   rstSort.Fields("用例编号").Value   &   ""   
                        bkstr(n)   =   rstSort.Fields("用例编号").Value   &   ""   
                        With   wddoc.Books   
                              .Add   Range:=wddoc.Tables(wddoc.Tables.Count).Rows.Last.Cells(2).Range,   Name:="bksmp"   &   n   
                              .ShowHidden   =   False   
                        End   With   
                        .Cells(3).Range.Text   =   rstSort.Fields("简要描述").Value   &   ""   
                        .Cells(4).Range.Text   =   FunPriIDToName(rstSort.Fields("优先级ID").Value)   &   ""   
                    End   With   
        
            Else   
        
      '直接添加一行数据   
                wddoc.Tables(wddoc.Tables.Count).Rows.Add   
                With   wddoc.Tables(wddoc.Tables.Count).Rows.Last   
                      .Range.Font.Bold   =   False   
                      .Range.Font.Name   =   "宋体"   
                      .Range.Font.Size   =   10   
                      .Cells(1).Range.Text   =   n   
                      .Cells(2).Range.Text   =   rstSort.Fields("用例编号").Value   &   ""   
                      bkstr(n)   =   rstSort.Fields("用例编号").Value   &   ""   
                      With   wddoc.Books   
                            .Add   Range:=wddoc.Tables(wddoc.Tables.Count).Rows.Last.Cells(2).Range,   Name:="bksmp"   &   n   
                            .ShowHidden   =   False   
                      End   With   
                      .Cells(3).Range.Text   =   rstSort.Fields("简要描述").Value   &   ""   
                      .Cells(4).Range.Text   =   FunPriIDToName(rstSort.Fields("优先级ID").Value)   &   ""   
                  End   With   
        
              End   If   
      '读取下一条记录   
          wddoc.UndoClear   
          rstSort.MoveNext   
      Loop   
        
      '创建详表   
        
      '定义标题   
      Set   rngrange   =   wddoc.Paragraphs(wddoc.Paragraphs.Count).Range   
      rngrange.InsertBreak   wdPageBreak   
      With   rngrange   
                .Font.Bold   =   True   
                .Font.Name   =   "隶书"   
                .Text   =   "详     表"   
                .Paragraphs.Alignment   =   wdAlignParagraphLeft   
                  .InsertParagraphBefore   
                .Collapse   wdCollapseEnd   
      End   With   
        
      '每个记录建立一个详表,同时每个系统的第一个记录前标示出系统名称   
      n   =   0   
      sysID   =   -1   
      rstSort.MoveFirst   
      Do   While   Not   rstSort.EOF   
            If   sysID   <>   rstSort.Fields("系统ID")   Then   
                  sysID   =   rstSort.Fields("系统ID")   
                  sysName   =   FunSysIDToName(sysID)   
            End   If   
            n   =   n   +   1   
      '画一详表   
            wddoc.Paragraphs.Add   
            Set   rngrange   =   wddoc.Paragraphs(wddoc.Paragraphs.Count).Range   
            With   rngrange   
                  .Tables.Add   rngrange,   7,   1   
                  .Tables(.Tables.Count).Style   =   "网格型"   
                  .Tables(.Tables.Count).Rows(2).Cells.Split   1,   2,   False   
                  .Tables(.Tables.Count).Rows(6).Cells.Split   1,   2,   False   
                  .Tables(.Tables.Count).Rows(6).Cells(1).SetWidth   250,   wdAdjustProportional   
                  .Tables(.Tables.Count).Rows(6).Cells(1).SetHeight   150,   wdAdjustProportional   
                  .Tables(.Tables.Count).Rows(3).Cells(1).SetHeight   60,   wdAdjustProportional   
                  .Tables(.Tables.Count).Rows(4).Cells(1).SetHeight   60,   wdAdjustProportional   
                  .Tables(.Tables.Count).Rows(5).Cells(1).SetHeight   100,   wdAdjustProportional   
                  .Tables(.Tables.Count).Rows(7).Cells(1).SetHeight   40,   wdAdjustProportional   
                  .Collapse   wdCollapseEnd   
            End   With   
      '向详表中填数据   
            With   rngrange.Tables(rngrange.Tables.Count)   
                        
                  .Range.Font.Bold   =   False   
                  .Range.Font.Size   =   10   
                  .Range.Font.Name   =   "宋体"   
                  
                  .Rows(1).Cells(1).Range.Text   =   "编号:   "   &   sysName   &   "_"   &   rstSort.Fields("用例编号")   
                  With   wddoc.Books   
                        .Add   Range:=wddoc.Tables(wddoc.Tables.Count).Rows.First.Cells(1).Range,   Name:="bkdtl"   &   n   
                        .ShowHidden   =   False   
                  End   With   
                  .Rows(2).Cells(1).Range.Text   =   "作者:   "   &   rstSort.Fields("作者")   
                  .Rows(2).Cells(2).Range.Text   =   "创建时间:   "   &   rstSort.Fields("创建时间")   
                  .Rows(3).Cells(1).Range.Text   =   "简要描述:   "   &   vbCrLf   &   rstSort.Fields("简要描述")   
                  .Rows(4).Cells(1).Range.Text   =   "测试目的:   "   &   vbCrLf   &   rstSort.Fields("测试目的")   
                  .Rows(5).Cells(1).Range.Text   =   "预制条件:   "   &   vbCrLf   &   rstSort.Fields("预制条件")   
                  .Rows(6).Cells(1).Range.Text   =   "测试步骤:   "   &   vbCrLf   &   rstSort.Fields("测试步骤")   
                  .Rows(6).Cells(2).Range.Text   =   "预期行为:   "   &   vbCrLf   &   rstSort.Fields("预期行为")   
                  .Rows(7).Cells(1).Range.Text   =   "备注:   "   &   vbCrLf   &   rstSort.Fields("备注")   
                  
              End   With   
              rngrange.InsertBreak   wdPageBreak   
              rstSort.MoveNext   
              wddoc.UndoClear   
          Loop   
        
      '创建超级链接并设置它的显示方式   
        
      With   wdapp.Options   
                '.CtrlClickHyperlinkToOpen   =   False   
                .SuggestSpellingCorrections   =   False   
                .CheckGrammarWithSpelling   =   False   
      End   With   
        
      

  4.   

    Dim   i   As   Long   
      Dim   str   As   String   
      For   i   =   1   To   n   
        
      '创建从简表测试用例编号到详表测试用例编号的链接   
          wddoc.Books("bksmp"   &   i).Range.Text   =   ""   
          wddoc.Hyperlinks.Add   Anchor:=wddoc.Books("bksmp"   &   i).Range,   Address:="",   _   
                      SubAddress:="bkdtl"   &   i,   TextToDisplay:=bkstr(i)   
                        
      '创建从详表测试用例编号到简表测试用例编号的链接   
          str   =   wddoc.Books("bkdtl"   &   i).Range.Text   
          str   =   Left(str,   Len(str)   -   2)   
          wddoc.Books("bkdtl"   &   i).Range.Text   =   ""   
          wddoc.Hyperlinks.Add   Anchor:=wddoc.Books("bkdtl"   &   i).Range,   Address:="",   _   
                      SubAddress:="bksmp"   &   i,   TextToDisplay:=str   
        
      '清空恢复操作,以释放word内存   
          wddoc.UndoClear   
        
      Next   
        
      '设置超级链接的字体样式   
      With   wddoc.Styles("超级链接").Font   
                      .NameFarEast   =   "宋体"   
                      .NameAscii   =   ""   
                      .NameOther   =   ""   
                      .Name   =   "Arial"   
                      .Size   =   10.5   
                      .Underline   =   wdUnderlineSingle   
                      .Color   =   wdColorBlue   
      End   With   
      wddoc.Styles("超级链接").BaseStyle   =   "默认段落字体"   
        
      '生成正式文档选中次数加1   
      If   strDocTp   =   "正式"   Then   
            rst.MoveFirst   
            Do   While   Not   rst.EOF   
                  rst.Edit   
                  rst.Fields("选中次数")   =   rst.Fields("选中次数")   +   1   
                  rst.Update   
                  rst.MoveNext   
            Loop   
      End   If   
        
      '释放对象   
      Set   rngrange   =   Nothing   
        
      End   If   
        
      End   Sub   
        
      Public   Function   FunSysIDToName(intSysID   As   Integer)   As   String   
      '============   
      '将系统ID转换为系统名称:如果不存在返回"";存在返回其名称。   
      '============   
      Dim   rst   As   Recordset   
              
      Set   rst   =   dbCaseDb.OpenRecordset("select   系统名称   from   tbl_sub_系统   where   系统ID="   &   intSysID)   
      If   rst.RecordCount   =   0   Then   
            FunSysIDToName   =   ""   
      Else   
            FunSysIDToName   =   rst.Fields("系统名称").Value   
      End   If   
      End   Function   
        
      Public   Function   FunPriIDToName(intPriID   As   Variant)   As   String   
      '============   
      '将优先级ID转换为优先级名称:如果不存在返回"";存在返回其名称。   
      '============   
      Dim   rst   As   Recordset   
      If   IsNull(intPriID)   Or   IsEmpty(intPriID)   Then   
            FunPriIDToName   =   ""   
            Else   
            Set   rst   =   dbCaseDb.OpenRecordset("select   优先级   from   tbl_sub_优先级   where   优先级ID="   &   intPriID)   
            If   rst.RecordCount   =   0   Then   
                  FunPriIDToName   =   ""   
            Else   
                  FunPriIDToName   =   rst.Fields("优先级").Value   
            End   If   
      End   If   
      End   Function   
        
      Sub   crtsmptbl(rst   As   Recordset)   
      '=================   
      '生成简表   
      '参数rst   中必须有的字段为:系统、编号、简要描述、优先级   
      '=================   
      Dim   wdapp   As   Word.Application   
      Dim   wddoc   As   Word.Document   
      Dim   rngrange   As   Word.Range   
      Dim   sys   As   String   
        
      '建立word应用程序对象   
      Set   wdapp   =   New   Word.Application   
      wdapp.Visible   =   True   
        
      '新建一个文档   
      Set   wddoc   =   wdapp.Documents.Add   
        
      '初始一个不可能的系统名称   
      sys   =   "*@*@$!"   
        
      '每个系统建立一个简表,并以系统名作为表的标题   
      Do   While   Not   rst.EOF   
            If   sys   <>   rst.Fields("系统")   Then   
            sys   =   rst.Fields("系统")   
        
      '定义标题   
      Set   rngrange   =   wddoc.Paragraphs(wddoc.Paragraphs.Count).Range   
      With   rngrange   
                .Font.Bold   =   True   
                .Font.Name   =   "隶书"   
                .Text   =   sys   
                .Paragraphs.Alignment   =   wdAlignParagraphLeft   
                  .InsertParagraphBefore   
                .Collapse   wdCollapseEnd   
      End   With   
        
      '添加表头   
      wddoc.Paragraphs.Add   
      Set   rngrange   =   wddoc.Paragraphs(wddoc.Paragraphs.Count).Range   
      With   rngrange   
                .Tables.Add   rngrange,   1,   6   
                .Tables(.Tables.Count).Style   =   "网格型"   
                .Tables(.Tables.Count).Columns(2).SetWidth   123,   wdAdjustProportional   
                .Collapse   wdCollapseEnd   
      End   With   
      With   wddoc.Tables(wddoc.Tables.Count).Rows.Last   
                .Range.Font.Bold   =   True   
                .Range.Font.Name   =   "宋体"   
                .Cells(1).Range.Text   =   "编号"   
                .Cells(2).Range.Text   =   "简要描述"   
                .Cells(3).Range.Text   =   "优先级"   
                .Cells(4).Range.Text   =   "测试结果"   
                .Cells(5).Range.Text   =   "测试时间"   
                .Cells(6).Range.Text   =   "备注"   
                .Range.Collapse   wdCollapseEnd   
      End   With   
        
      '添加一行数据   
      wddoc.Tables(wddoc.Tables.Count).Rows.Add   
      With   wddoc.Tables(wddoc.Tables.Count).Rows.Last   
                .Range.Font.Bold   =   False   
                .Range.Font.Name   =   "宋体"   
                .Range.Font.Size   =   10   
                .Cells(1).Range.Text   =   rst.Fields("编号").Value   &   ""   
                .Cells(2).Range.Text   =   rst.Fields("简要描述").Value   &   ""   
                .Cells(3).Range.Text   =   rst.Fields("优先级").Value   &   ""   
      End   With   
        
      Else