请教,想用vb和access,及word模板格式化输出“合同”,有源代码看不懂,能否帮我解释一下

lhbhg 2008-03-07 11:11:08
网上考的但看不懂,谢谢解释一下,我是菜鸟
运行时出现“未定义类”
我按这个步骤做:
(1)建word模板文档
(2)在vb6.0中将下面的代码输入
(3)是不是还要自己建个access数据表,再定义字符

能否推荐一本有下列例子的教材,谢谢

用vb+word模版+数据库来制作格式合同的方法
概述:在应用程序中经常有定制格式报表的需要,如打印合同、货物清单、备忘录等等,使用第三方报表软件可以实现但是比较繁琐,实际上利用word的自动化编程,使用vba可以完成类似的功能,而且很实用。
步骤一、
word模版制作:
在第一行是合同标题 " 【书签1合同标题xxxxxxxx合同】"
第二行
******************************
合同编号: 【书签2合同编号】
签约单位: 【书签3签约单位】
签约地址: 【书签4签约地址】
签约日期: 【书签5签约日期】.....

表格第一行 '表格第一行第一列中插入 书签4
表格第二行
'实现代码如下
dim cn as new adodb.connection
dim adors as new adodb.recordset
dim wordtemps as new word.application
private sub form_load()
if cn.state = 1 then
cn.close
end if
cn.cursorlocation = aduseclient
cn.open "provider=microsoft.jet.oledb.4.0;data source=" & app.path & "\db1.mdb"
end sub
'开始导出数据
private sub command1_click()
dim strsql as string
dim rec as integer
dim i as integer
wordtemps.documents.add app.path + "\货物合同.doc", false
wordtemps.selection.goto wdgotobookmark, , , "合同标题"
wordtemps.selection.typetext “关于冬季货物的成交合同”
wordtemps.selection.goto wdgotobookmark, , , "合同编号"
wordtemps.selection.typetext “2004000001”
wordtemps.selection.goto wdgotobookmark, , , "签约单位"
wordtemps.selection.typetext “宏大科技公司,天天科技公司”
wordtemps.selection.goto wdgotobookmark, , , "签约地址"
wordtemps.selection.typetext “北京中关村大厦”
wordtemps.selection.goto wdgotobookmark, , , "签约时间"
wordtemps.selection.typetext fromat(now, "yyyy-mm-dd")


strsql = "select * from matrixs"
adors.open strsql, cn, adopenkeyset, adlockoptimistic
rec = adors.recordcount
if rec < 1 then
msgbox "无商品记录!", vbokonly, "提示"
adors.close
exit sub
else
adors.movefirst
wordtemps.selection.goto wdgotobookmark, , , "货物清单"
for i = 1 to rec
wordtemps.selection.typetext adors!名称
wordtemps.selection.moveright unit:=wdcharacter, count:=1 '右移一格
wordtemps.selection.typetext adors!数量
wordtemps.selection.moveright unit:=wdcharacter, count:=1 '右移一格
wordtemps.selection.typetext adors!规格
adors.movenext
if adors.eof = false then
wordtemps.selection.insertrowsbelow 1 '表格换行
end if
next i
adors.close
wordtemps.visible = true '显示word窗口
end if
end sub


作者:soho_andy(冰)
...全文
505 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
vansoft 2012-02-01
  • 打赏
  • 举报
回复
作个模板。用邮件合并功能。

下次直接打开,生成一下就行了。
huimartin 2012-01-27
  • 打赏
  • 举报
回复
用vb+word模版+数据库来制作格式合同的方法
概述:在应用程序中经常有定制格式报表的需要,如打印合同、货物清单、备忘录等等,使用第三方报表软件可以实现但是比较繁琐,实际上利用word的自动化编程,使用vba可以完成类似的功能,而且很实用。
步骤一、
word模版制作:

注意合同模板的制作,要使用书签的,因为在程序中进行了引用在第一行是合同标题 " 【书签1合同标题xxxxxxxx合同】"
第二行
******************************
合同编号: 【书签2合同编号】
签约单位: 【书签3签约单位】
签约地址: 【书签4签约地址】
签约日期: 【书签5签约日期】.....

表格第一行 '表格第一行第一列中插入 书签4
表格第二行
'实现代码如下
定义数据库的链接dim cn as new adodb.connection
dim adors as new adodb.recordset
dim wordtemps as new word.application
private sub form_load()
if cn.state = 1 then
cn.close
end if
cn.cursorlocation = aduseclient
cn.open "provider=microsoft.jet.oledb.4.0;data source=" & app.path & "\db1.mdb"
end sub
'开始导出数据
private sub command1_click()
dim strsql as string
dim rec as integer
dim i as integer
wordtemps.documents.add app.path + "\货物合同.doc", false
wdgotobookmark就是书签的引用
wordtemps.selection.goto wdgotobookmark, , , "合同标题"
wordtemps.selection.typetext “关于冬季货物的成交合同”
wordtemps.selection.goto wdgotobookmark, , , "合同编号"
wordtemps.selection.typetext “2004000001”
wordtemps.selection.goto wdgotobookmark, , , "签约单位"
wordtemps.selection.typetext “宏大科技公司,天天科技公司”
wordtemps.selection.goto wdgotobookmark, , , "签约地址"
wordtemps.selection.typetext “北京中关村大厦”
wordtemps.selection.goto wdgotobookmark, , , "签约时间"
wordtemps.selection.typetext fromat(now, "yyyy-mm-dd")


strsql = "select * from matrixs"
adors.open strsql, cn, adopenkeyset, adlockoptimistic
rec = adors.recordcount
if rec < 1 then
msgbox "无商品记录!", vbokonly, "提示"
adors.close
exit sub
else
adors.movefirst
wordtemps.selection.goto wdgotobookmark, , , "货物清单"
for i = 1 to rec
wordtemps.selection.typetext adors!名称
wordtemps.selection.moveright unit:=wdcharacter, count:=1 '右移一格
wordtemps.selection.typetext adors!数量
wordtemps.selection.moveright unit:=wdcharacter, count:=1 '右移一格
wordtemps.selection.typetext adors!规格
adors.movenext
if adors.eof = false then
wordtemps.selection.insertrowsbelow 1 '表格换行
end if
next i
adors.close
wordtemps.visible = true '显示word窗口
end if
end sub
dillionwang 2008-06-03
  • 打赏
  • 举报
回复
你的各种引用都按要求做了么
dillionwang 2008-06-03
  • 打赏
  • 举报
回复
不错 前半部分就是自动填写的,好像
后面主要strsql = "select * from matrixs"
adors.open strsql, cn, adopenkeyset, adlockoptimistic
rec = adors.recordcount
if rec < 1 then
msgbox "无商品记录!", vbokonly, "提示"
adors.close
exit sub
else
adors.movefirst
wordtemps.selection.goto wdgotobookmark, , , "货物清单"
for i = 1 to rec
wordtemps.selection.typetext adors!名称
wordtemps.selection.moveright unit:=wdcharacter, count:=1 '右移一格
wordtemps.selection.typetext adors!数量
wordtemps.selection.moveright unit:=wdcharacter, count:=1 '右移一格
wordtemps.selection.typetext adors!规格
adors.movenext
if adors.eof = false then
wordtemps.selection.insertrowsbelow 1 '表格换行
end if
next i
adors.close
wordtemps.visible = true '显示word窗口
end if
实现数据的写入
百易通 2008-05-13
  • 打赏
  • 举报
回复
收藏!谢谢!
------------------
IOAS:易学易用、灵活自由、个人免费使用的办公系统速成工具。
详情请访问:www.ioas.net
------------------
pingjinzhao 2008-05-05
  • 打赏
  • 举报
回复
去看看vba
knowledge_Is_Life 2008-05-01
  • 打赏
  • 举报
回复
以后需再关注,现在先帮你顶一下
meiZiNick 2008-04-30
  • 打赏
  • 举报
回复
不明LZ在说什么
junki 2008-03-10
  • 打赏
  • 举报
回复
太长了,看了头昏
自己先整理一下吧,哪些不懂再问

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧