加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 站长学院 > Asp教程 > 正文

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

发布时间:2023-08-01 15:15:01 所属栏目:Asp教程 来源:
导读:在mis系统的实际开发中,我们有时需要将当前页面上报表的数据以word文档的格式下载到本地,这种实现并不困难。但是有时我们需要对下载的word文档的格式做一些设置,比如标题颜色,字体大小,字间距等等,这时我们就要
在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组件,有了一些了解。只要多使用,就会很快熟悉的。
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章