关于一个VB操作WORD的程序,遇到的问题,搞了两天了,求教大家啊

wangwei244157887 2010-04-22 12:37:41
我利用VB操作WORD,往WORD的一些表里面写一些数据,

Private Sub printord_Click()

'按照指定格式打印订单文档Contract.doc
Dim wdoc As Object '定义WORD文件对象
Dim wapp As Object '定义word应用程序
Dim mytable As Object
Dim arow As Object
Dim wordfile As String

wordfile = "Contract.doc" '文档名称
'If Dir(wordfile) = "" Then '判断文件是否存在
'MsgBox "打印文件Contract.doc丢失,请与管理员联系"
'Exit Sub
'End If
'启动应用程序
Set wapp = CreateObject("Word.Application") '王伟修改
wapp.Visible = True
'打印过程可见
'打开Contract.doc文件
Set wdoc = wapp.Documents.Open(Apppath + "\" + wordfile, ReadOnly:=True, Revert:=True)
'读写订单编号
wdoc.Tables(1).Cell(2, 1).Range.Delete
wdoc.Tables(1).Cell(2, 1).Range.InsertAfter "订单编号:" + Trim(orderID.Caption)
'订单打印时间
wdoc.Tables(1).Cell(2, 2).Range.Delete
wdoc.Tables(1).Cell(2, 2).Range.InsertAfter "打印时间:" + Trim(str(Now))

'输出订单信息
wdoc.Tables(2).Cell(1, 2).Range.InsertAfter Trim(newtime.Text) '创建日期
wdoc.Tables(2).Cell(1, 4).Range.InsertAfter Trim(contime.Text) '交货日期
wdoc.Tables(2).Cell(2, 2).Range.InsertAfter Trim(sample.Text) '样品类型
wdoc.Tables(2).Cell(2, 4).Range.InsertAfter Trim(total.Text) '合计
wdoc.Tables(2).Cell(3, 2).Range.InsertAfter Trim(price.Text) '单价
wdoc.Tables(2).Cell(3, 4).Range.InsertAfter Trim(amount) '数量
wdoc.Tables(2).Cell(4, 2).Range.InsertAfter Trim(discount.Text) '折扣
wdoc.Tables(2).Cell(4, 4).Range.InsertAfter Trim(earnestMoney.Text) '已交定金
wdoc.Tables(2).Cell(5, 2).Range.InsertAfter Trim(ordernote.Text) '备注

'商家信息
shop.selectinfo
wdoc.Tables(3).Cell(1, 2).Range.InsertAfter Trim(rs.Fields(0)) '店名
wdoc.Tables(3).Cell(1, 4).Range.InsertAfter Trim(rs.Fields(1)) '联系人
wdoc.Tables(3).Cell(2, 2).Range.InsertAfter Trim(rs.Fields(2)) '座机
wdoc.Tables(3).Cell(2, 4).Range.InsertAfter Trim(rs.Fields(3)) '手机
wdoc.Tables(3).Cell(3, 2).Range.InsertAfter Trim(rs.Fields(4)) '邮箱
wdoc.Tables(3).Cell(3, 4).Range.InsertAfter Trim(rs.Fields(5)) 'qq
wdoc.Tables(3).Cell(4, 2).Range.InsertAfter Trim(rs.Fields(6)) '地址
CloseRs

'客户信息
wdoc.Tables(4).Cell(1, 2).Range.InsertAfter Trim(customerID.Caption) '客户编号
wdoc.Tables(4).Cell(1, 4).Range.InsertAfter Trim(customername.Text) '客户姓名
wdoc.Tables(4).Cell(2, 2).Range.InsertAfter Trim(customersex.Text) '性别
wdoc.Tables(4).Cell(2, 4).Range.InsertAfter Trim(customerpost.Text) '邮编
wdoc.Tables(4).Cell(3, 2).Range.InsertAfter Trim(customerIDCard.Text) '身份证号
wdoc.Tables(4).Cell(3, 4).Range.InsertAfter Trim(name3d.Text) '相关文件
wdoc.Tables(4).Cell(4, 2).Range.InsertAfter Trim(customertel.Text) '联系电话
wdoc.Tables(4).Cell(4, 4).Range.InsertAfter Trim(customerpost.Text) '邮编
wdoc.Tables(4).Cell(5, 2).Range.InsertAfter Trim(customeraddr.Text) '地址
wdoc.Tables(4).Cell(6, 2).Range.InsertAfter Trim(customercom.Text) '工作单位
wdoc.Tables(4).Cell(7, 2).Range.InsertAfter Trim(customernote.Text) '备注
Cancel:
Set wdoc = Nothing '清空文件对象
Set wapp = Nothing '清空应用程序对象
End Sub



当进行上述代码操作的时候,有时候连续的进行几次也没问题,有时候却连续的报错,错误主要分为以下三种:

run_time error'462'
the remote server machine does not exist or is unavailable

run_time error'430'
class does not support automation or does not support expected interface

run_time error'-2147417848(80010108)'
automation error the object invoked has disconnected from its clients

我觉得这些问题是同一个原因引起的,快崩溃了,搞了两天,请大家指点一下,到底是哪里错了,是不是对象声明有问题?
...全文
571 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
ning1958 2010-04-27
  • 打赏
  • 举报
回复
在club.ExcelHome.net网站有个守柔版主,针对word有很多帖子,你可以借鉴.
「已注销」 2010-04-27
  • 打赏
  • 举报
回复
我点取消,没有进行保存,呵呵,无任何操作,只是关闭
「已注销」 2010-04-27
  • 打赏
  • 举报
回复

Dim wdoc As Object '定义WORD文件对象
Dim mytable As Object
Dim arow As Object
Dim wordfile As String

On Error GoTo ErrorHandler:
wordfile = "Contract.doc" '文档名称
Set wapp = CreateObject("Word.Application")
Set wapp = New Word.Application
wapp.Visible = True
Set wdoc = wapp.Documents.Open(Apppath + "\" + wordfile, ReadOnly:=True, Revert:=True)
。。。。。。。。。
'Set wdoc = Nothing '清空文件对象
'Set wapp = Nothing '清空应用程序对象
ErrorHandler:
If InStr(1, Err.Description, "ActiveX component can't create object") <> 0 Then
MsgBox "请检查是否装有Word应用程序?" + Chr(10) + Chr(13) + "打印订单不能进行!", vbOKOnly + vbCritical, "错误提示"
Exit Sub
End If
MsgBox Err.Description
Set wdoc = Nothing '清空文件对象
Set wapp = Nothing '清空应用程序对象

后来这样修改的,因为当时想的是,有可能是我word文档还没关闭就对其进行清除,导致这个问题,后来发现这样写以后,Err.Description。
另外想到的一个办法就是声明Dim WithEvents wapp As Word.Application '定义word应用程序
然后在Quit事件里面,清除对象,发现还是报这个错误,我现在就做了个异常处理,程序就不崩溃了
舉杯邀明月 2010-04-26
  • 打赏
  • 举报
回复
在 Set wdoc = Nothing '清空文件对象 之前,你不关闭文档么?
ningweidong 2010-04-25
  • 打赏
  • 举报
回复
关注,我也想用vb操作word
bcrun 2010-04-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 wangwei244157887 的回复:]

Set wdoc = wapp.Documents.Open(Apppath + "\" + wordfile, ReadOnly:=True, Revert:=True)
文件名后面的那些参数不要的?
[/Quote]

楼主的学习方法可能有问题,具体到那些参数的含义,你不知道可以直接查帮助啊,比到处问快得多,关键是查这个又不要啥复杂的技术:)
关于你用的ReadOnly:=True,我都看到已有两人质疑了。至于具体怎么改,是选择打开时去年此参数,还是保存时使用另外的文件名,那需要你自己按需求来决定了。
贝隆 2010-04-25
  • 打赏
  • 举报
回复
UPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUPUP
xiaowei820312 2010-04-25
  • 打赏
  • 举报
回复
word里的操作可以使用vba
liguicd 2010-04-22
  • 打赏
  • 举报
回复
帮顶了...
king06 2010-04-22
  • 打赏
  • 举报
回复
只读的能保存吗...
「已注销」 2010-04-22
  • 打赏
  • 举报
回复
Set wdoc = wapp.Documents.Open(Apppath + "\" + wordfile, ReadOnly:=True, Revert:=True)
文件名后面的那些参数不要的?
「已注销」 2010-04-22
  • 打赏
  • 举报
回复
这是以前我同事的程序,我修改的,我网上了查了一下,打开的时候参数好像只要文件名,就行了吗?后面的参数不需要的吗?还有就是我打开以后一般打印了,然后点击WORD的关闭按钮后,点击否不保存的,然后返回的时候就出现上面的问题啊
bcrun 2010-04-22
  • 打赏
  • 举报
回复
楼主先别说那么多,赶紧把ReadOnly:=True 和你怎么保存文件的总是讲清楚再说吧:)
「已注销」 2010-04-22
  • 打赏
  • 举报
回复
这些错误都是WORD关闭的时候发生错误的,是不是要释放什么东西
chuifengde 2010-04-22
  • 打赏
  • 举报
回复
ReadOnly:=True   ?
Tiger_Zhao 2010-04-22
  • 打赏
  • 举报
回复
你打开 Contract.doc 进行修改后即没有另存为,又没有取消修改关闭。
这样再次打开同一个文件会报错。

还有注意一下是否存在多个 Office 版本,或者安装新版本前老版本没有卸载干净。
jabulin 2010-04-22
  • 打赏
  • 举报
回复
关注,帮顶......

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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