在VB中利用Word宏命令开发ASP组件

1/8/2004来源:ASP实例人气:23781

作者:杨忠勋
专业:计算机软件开发及应用
语言能力: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组件,有了一些了解。只要多使用,就会很快熟悉的。