首 页 网站运营 网络编程 网页制作 图象媒体 数据库 建站资源 网管专区 下载专区 最新资讯
IT学堂|红色黑客联盟
设为首页
加入收藏
联系站长
您所在的位置:首页>网络编程>ASP>文章内容
在VB中利用Word宏命令开发ASP组件
来源: 作者: 发布时间:2007-04-12

语言能力:TOFEL633 GRE2140
Email:zhongxunyang@yahoo.com.cn

      在Mis系统的实际开发中,我们有时需要将当前页面上报表的数据以Word文档的格式下载到本地,这种实现并不困难。但是有时我们需要对下载的Word文档的格式做一些设置,比如标题颜色,字体大小,字间距等等,这时我们就要用到Word自带的宏功能。

      比如我们想将此报表的标题在Word文档中以如下格式显示:14号字,加粗,居中对齐。首先我们需要在Word中录制相应的宏命令。打开Word,新建一文档,手动敲入一行字,然后选择工具->宏->录制新宏命令,为新宏取一个名字如Macro1,执行以上动作(14号字,加粗,居中对齐),Word自动将这些动作保存以相应的Vbscript命令。然后选择工具->宏->宏命令,选择刚才我们定义的宏Macro1,就可以查看其内容了。在此例中我们保存的宏命令如下:   

    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter '居中对齐
    Selection.Font.Bold = wdToggle '加粗显示
    Selection.Font.Size = 14 '14号字

    因为宏命令的脚本语言是Vbscript,我们不需要做任何改动就可以将上面的语句在VB中使用。这样,我们就可以编写出如下VB代码,实现我们所要求的功能。代码如下:

    WdApp.Selection.Font.Bold = wdToggle '加粗显示
    WdApp.Selection.Font.Size = 14 '14号字
    WdApp.Selection.TypeText ("报表标题") '报表标题
    WdApp.Selection.ParagraphFormat.lignment = wdAlignParagraphCenter '居中对齐
    WdApp.Selection.Font.Bold = wdToggle '取消加粗

    同样,我们如想对Word文档进行其他处理,重复以上的步骤就可以了。以下提供我的一个完整的对Word文档进行处理的例子:

    Private Function SaveAsWord(ByRef MyRecord As Recordset, ByVal DocFileName As String, ByRef OutMessage As String) As Integer
    '*************************************************************************
    '
    '说明:将数据集中的数据另存为DOC文件
    '
    '参数:
    '
    'MyRecord       数据集
    'DocFileName    WORD文件的名称(无路径,路径见实例变量sPath)
    'OutMessage     操作的的返回信息
    '
    '返回:         1成功   -1失败
    '
    '*************************************************************************
   
    '初始化Word应用
    err.Clear
    On Error GoTo Err_All
    Dim WdApp As Word.Application
    Set WdApp = CreateObject("Word.Application")

    '插入数据
    Dim colloop As Integer      '列号
    Dim rowloop As Integer      '行号
    Dim colMax As Integer       '列数
    Dim rowMax As Integer       '行数
    Dim wdcell As Integer       '宽
    Dim UnitEnd As Integer      '截取结束点
    Dim UnitName As String      '单位名称
    Dim BbDate As String        '报表期别
   
   
    wdcell = 12
    colMax = MyRecord.Fields.count
    rowMax = MyRecord.RecordCount

    WdApp.Documents.Add
   
    '获取报表单位
    UnitEnd = InStr(sBBDetail, "期别")
    UnitName = Mid(sBBDetail, 1, UnitEnd - 2)
    BbDate = Mid(sBBDetail, UnitEnd, Len(sBBDetail))
   
    If MyRecord.Fields.count >= 10 Then
        WdApp.ActiveDocument.PageSetup.Orientation = wdOrientLandscape
    Else
        WdApp.ActiveDocument.PageSetup.Orientation = wdOrientPortrait
    End If
   
    '报表名称
    WdApp.Selection.Font.Bold = wdToggle
    WdApp.Selection.Font.Size = 14
    WdApp.Selection.TypeText (sbbmc)
    WdApp.Selection.ParagraphFormat.lignment = wdAlignParagraphCenter
    WdApp.Selection.Font.Bold = wdToggle
    WdApp.Selection.TypeParagraph


   
    '报表单位名称
    WdApp.Selection.Font.color = wdColorBlack
    WdApp.Selection.Font.Size = 11
    WdApp.Selection.TypeText (UnitName)
    WdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    WdApp.Selection.TypeParagraph
   
    '报表期别
    WdApp.Selection.TypeText (BbDate)
    WdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
    WdApp.Selection.TypeParagraph
    WdApp.Selection.TypeParagraph
   
    '生成列头
    'wdApp.Selection.HomeKey wdLine, wdExtend
    'dApp.Selection.Font.Bold = wdToggle

    WdApp.ActiveDocument.Tables.Add WdApp.Selection.Range, rowMax, colMax
    Dim i As Integer
    Do
        For colloop = 0 To colMax - 1
            WdApp.Selection.Font.Size = 9
       
            If i = 0 Then
            
                '表格中标题加粗显示
                WdApp.Selection.Font.Bold = wdToggle
               
                '表格标题行背景颜色设置为灰色,灰度为30
                With WdApp.Selection.Cells
                     With .Shading
                          .Texture = wdTextureNone
                          .ForegroundPatternColor = wdColorAutomatic
                          .BackgroundPatternColor = wdColorGray30
                     End With
                End With
           
            End If
            '最后一行右对齐,其余左对齐
            If i > 0 Then
               If MyRecord.Fields.Item(colloop).Name = "ZBMC" Or MyRecord.Fields.Item(colloop).Name = "指标名称" Then
                  WdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
               Else
                  WdApp.Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
               End If
            End If
           


           
            If i = 0 And (MyRecord.Fields.Item(colloop).Name = "SXH" Or MyRecord.Fields.Item(colloop).Name = "顺序号") Then
                WdApp.Selection.TypeText ("序号")
            Else
                WdApp.Selection.TypeText (CStr(MyRecord.Fields.Item(colloop).value))
            End If
            If (i <> rowMax - 1 Or (i = rowMax - 1 And colloop < colMax - 1)) Then
               WdApp.Selection.MoveRight (wdcell)
            End If
        Next
        i = i + 1
        MyRecord.MoveNext
    Loop Until MyRecord.EOF
   
    WdApp.ActiveDocument.SaveAs DocFileName, 0, False, "", True, "", False, False, False, False, False
    WdApp.Quit
   
    SaveAsWord = 1
    Exit Function
       
Err_All:
    Set WdApp = Nothing
    SaveAsWord = -1
    OutMessage = err.Description
    Exit Function
End Function

    好了,到此为止,我想你们对在VB中利用Word宏命令开发ASP组件,有了一些了解。只要多使用,就会很快熟悉的。

转自:动态网制作指南 www.knowsky.com



[推荐] [返回顶部] [打印本页] [关闭窗口]
热点文章
·ASP如何获取客户端真实IP地址
·ASP实现可显示和隐藏的树型菜单
·用ASP动态生成JS的表单验证代码
·ASP:用Asp编程实现QQ的在线情况查询
·ASP:用ASP打造一个小型的网页BBS系统
·ASP:用ASP编程实现网络内容快速查找
·ASP:利用ASP把图片上传到数据库
·ASP无法更新ACCESS数据库解决方法
·如何对ASP.NET进行性能优化
·入门:防范SQL注入攻击的新办法
相关文章
·无刷新随时取得用户当前活动信息
·浅谈TeeChart组件在ASP中的应用
·浅谈TeeChart组件在ASP中的应用
·DW+ASP 玩转动态二级菜单
·无组件实现文件上传/下载
·编写一个asp代码执行器
·ASP操作XML文件的完整实例
·ASP + Serv-u 实现FTP的代码
·如何使用FSO搜索硬盘文件
·ASP用JMail、CDO发送邮件
文章检索
Google
相关文章
·无刷新随时取得用户当前
·浅谈TeeChart组件在ASP
·浅谈TeeChart组件在ASP
·DW+ASP 玩转动态二级菜
·无组件实现文件上传/下
·编写一个asp代码执行器
·ASP操作XML文件的完整实
·ASP + Serv-u 实现FTP的
·如何使用FSO搜索硬盘文
·ASP用JMail、CDO发送邮
·利用客户端js实现汉字简
·在ASP中使用FSO组件生成
·金额大小写转换的asp完
·利用XSL和ASP在线编辑XM
·用ASP学做一个在线调查