欢迎光临
我们一直在努力

在VB中利用Word宏命令开发ASP组件-ASP教程,组件开发

建站超值云服务器,限时71元/月

在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组件,有了一些了解。只要多使用,就会很快熟悉的。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 在VB中利用Word宏命令开发ASP组件-ASP教程,组件开发
分享到: 更多 (0)

相关推荐

  • 暂无文章