频道直达 - 学院 - 下载 - 交易 - 截图 - 特效 - 字库 - 手册 - 排名-工具 - 繁體
设为首页
加入收藏
联系我们
建站搜索: 虚拟主机  域名注册   常用广告代码      用户注册 | 用户登陆
广告加载中...
您当前的位置:中国建站之家 -> 网站开发 -> ASP -> 文章内容
精彩推荐
热门文章
· 注册码大全二
· 注册码大全四
· 注册码大全一
· [图文] 头像-qq头像(..
· 要10G免费网络硬盘的..
· 注册码大全三
· 注册码大全十
· [图文] 梦幻背景图片..
· [图文] 卡通动物图片..
· [图文] 风景图片8
相关文章
· 38%恶意程序可在Vi..
· 可安装的ASP组件2
· 可安装的ASP组件1
· 怎样在vc、delphi、..
· 在vs3下调试无误的p..
· 使用ASP组件的一点经..
· 在VC++ 编写的组件中..
· 在VB组件中使用串缓..
· 在VB组件中使用串缓..
· 在VB组件中使用串缓..
在VB中利用Word宏命令开发ASP组件
作者:未知  来源:转载  发布时间:2005-9-16 0:53:46  发布人:acx

减小字体 增大字体

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

[打 印]
[] [返回上一页] [收 藏]
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图 - 人才招聘
网站合作、内容监督、商务咨询:QQ: 9576619
Copyright ? 2005--2008 中国建站之家版权所有
未经授权禁止转载、摘编、复制或建立镜像.如有违反,追究法律责任.
免责申明:中国建站之家(www.jz123.cn)上的所有提供下载的软件和资源
均来源于网络,为软件或程序作者提供和网友推荐收集整理而来,仅供学习
和研究使用。如有侵犯你的版权,请立即联系我们,本站将在3个工作日内删除。
粤ICP备05092265号