




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、追加500分求教 vb操作word问题悬赏分:200 - 解决时间:2007-7-28 11:42说明,用vb操作word,以下源码第一次操作完全正常,正常打开,正常替换,正常退出。进程中并没有留下windword的进程,但第二次继续操作时就出问题,运行到replaceword()就出现462错误,关闭程序重新开始又正常。请指教出错及解决原因,追加到500分 '=打开word= function openword(filename) '打开指定word文档 dim wordapp as new word.application dim worddoc as new word.d
2、ocument set wordapp = createobject("word.application") wordapp.visible = false set worddoc = wordapp.documents.open(filename) end function =替换关键字= function replaceword(searchstr, replacestr) '全部替换函数 selection.find.clearformatting selection.find.replacement.clearformatting with selectio
3、n.find .text = searchstr .replacement.text = replacestr .forward = true .wrap = wdfindcontinue .format = false .matchcase = false .matchwholeword = false .matchbyte = true .matchwildcards = false .matchsoundslike = false .matchallwordforms = false end with selection.find.execute replace:=wdreplaceal
4、l end function '=另存为= function saveasword(diskstr, namestr) changefileopendirectory diskstr activedocument.saveas filename:=namestr, fileformat:=wdformatdocument _ , lockcomments:=false, password:="", addtorecentfiles:=true, _ writepassword:="", readonlyrecommended:=false, em
5、bedtruetypefonts:=false, _ savenativepictureformat:=false, saveformsdata:=false, saveasaoceletter:= _ false application.documents.close application.quit end function '=清除对象= function closeword() set worddoc = nothing '清除文件实例 set wordapp = nothing '清除word实例 end function问题补充:根据小fisher的答案,已
6、经解决问题,另外在saveasword过程中,changefileopendirectory diskstr更改为changefileopendirectory diskstr ,activedocument.saveas更改为worddoc.saveas,再加上原先小fisher提到要更改的地方,已经完美解决,谢谢! ps:由于百度只能2次提高悬赏,每次50分,所以现在只有200分,未能对现500分的诺言,所以只有另开贴来加送300分!提问者: 有野问 - 经理 五级 最佳答案1) function replaceword(searchstr, replacestr) '全部替换函数
7、 selection.find.clearformatting selection.find.replacement.clearformatting with selection.find . 这个函数过程有错误!因为selection是word的对象而不是vb的对象,所以不能在vb中直接引用,必须用wordapp.selection替换掉selection才行! 这段代码在word vba中调试不会出问题,但移植到vb中时要注意。 2)wordapp和worddoc必须定义为全局变量,在模块公有部分使用 dim wordapp as word.application dim worddoc
8、as word.document 来定义 然后在openword(filename)过程中使用set wordapp =new word.application 和set worddoc = wordapp.documents.open(filename),否则在closeword()过程中wordapp和worddoc两个变量将会是未初始化的variant类型。 3)function saveasword(diskstr, namestr)中的application须使用wordapp替换,道理同1)。_-网上说 在visual basic中创建word文档使用语句 dim newdoc a
9、s word.document set newdoc = new word.document 但是vb会提示上述语句 “用户定义类型未定义” 另一种创建的方法是 dim msword as object set msword = createobject("word.basic") msword.appshow msword.filenewdefault 可以创建 但是下述语句没办法使用(即对该文档的一些操作) with msword .content.font.name = "宋体" .content.font.size = 12 .content.p
10、aragraphs.linespacing = 15.5 end with 请教各位高手指点,我想在vb中创建一个word文档,并将程序计算的一堆结果,在该文档中按一定的格式打印出来。 另,我在word中录制了一段宏,请问在vb中如何调用,要求代码 sub zz() activedocument.tables.add range:=selection.range, numrows:=6, numcolumns:= _ 6, defaulttablebehavior:=wdword9tablebehavior, autofitbehavior:= _ wdautofitfixed with se
11、lection.tables(1) if .style "网格型" then .style = "网格型" end if .applystyleheadingrows = true .applystylelastrow = true .applystylefirstcolumn = true .applystylelastcolumn = true end with selection.typetext text:="1" selection.moveright unit:=wdcell selection.typetext text
12、:="2" selection.moveright unit:=wdcell selection.typetext text:="3" selection.moveright unit:=wdcell selection.typetext text:="4" selection.moveright unit:=wdcell selection.typetext text:="5" end sub提问者: zhaizhaiya - 助理 三级 最佳答案将 with msword .content.font.name
13、= "宋体" .content.font.size = 12 .content.paragraphs.linespacing = 15.5 end with 改写为 msword.content.font.name = "宋体" msword.content.font.size = 12 msword.content.paragraphs.linespacing = 15.5 就可以了。 _-用vb读word时,在程序读完,为什么还有关联,怎样给她关联除啦悬赏分:0 - 解决时间:2008-10-23 10:06用vb实现读word程序的操作! 也就是说
14、当我运行vb程序时读啦一次word 第二次再执行时还是第一次的结果! 例如执行下面这个语句时: if newdoc.paragraphs(1).range.font.name = "新 宋 体" then s = s + 0.5 x = msgbox("s=" + str$(s), 64, "字体为1.5") 执行第一次之后,再改程序,执行时,总是提示word在另一个地方已经打开,实际上没有打开,我想这是由于关联的问题!提问者: lcs6678 - 助理 二级 最佳答案在任务管理器下的进程下看看,肯定打开了。 不知道你是用什么方法定义
15、的word对象。 如果这样定义: dim a as object, b as object, c as object set a = createobject("word.application") set b = a.documents.open("c:1 .doc") 用以下语句关闭 b.close a.quit _在vb如何存取word格式的文件悬赏分:30 - 解决时间:2006-9-12 08:30我想把用vb计算的一堆结果打印在word里面,并且有固定格式的简单排版,最好能在word中打印一些表格,请问用什么样的函数或者对象实践?希望高手帮忙
16、,要是能加一点说明,更加感激不尽提问者: zhaizhaiya - 助理 二级 最佳答案我帮你搜索到了两种方法 虽然我自己也不懂 但是也许你会懂吧 通过vba进行word调用,要替换word中固定位置的值,可以事先在文档中定义标签,然后修改标签的内容即可,不用查找。 vb中用vba调用word的方法: 菜单-工程-引用->microsoft word 9.0 object library (后面的数字为版本号) dim wdapp as new word.application dim docapp as new word.document 具体用怎么用标签替换,可以到word中录制宏看
17、看就知道了,宏可以直接在vb中通过vba执行。 方法2 使用vb编程时,有时需要调用microsoft word对文字进行编辑、排版及输出。为实现这种调用,可以使用shell函数、ole自动化、在包容器中嵌入word对象等方法。经过试用和比较,总结出了这几种方法的各自特点。 1 使用shell函数直接调用 语法:shell (pathname,windowstyle).pathname是指要执行的程序的名字和任何必须的参数或命令行开关,可以包括目录和驱动器名;windowstyle是执行程序的窗口风格的数字。 使用shell调用word比较简单,编程量小,但必须明确指定word所在路径,这不利
18、于移植,而且,不能对word进行控制,不利于程序和word之间的数据交换。 2 使用ole自动化控制microsoft word 2.1 使用方法 (1)word为ole自动化提供一种称为“basic”的对象,要在vb中控制word ,首先要定义一个引用word中“basic”对象的对象变量:dim wordobj as object (2)将word 中的“basic”对象赋给该对象:set wordobj=createobject("word.basic") (3)可以使用大多数wordbasic语句和函数控制word或word文档,使用方法和在word宏中使用word
19、basic指令的方法基本相同。 (4)关闭word:set wordobj =nothing。 注意:“basic”对象不支持关闭它自己的一个方法。即若在ole自动化中关闭了word,则对象被置为nothing,便不能再对对象进行操作,程序出错。 2.2 vb指令与wordbasic指令的差异 (1)有一些语句和函数不能使用,包括:控制结构,如whilewend和ifthenelse;声明语句,如dim;定制对话框相关的语句:fileexit语句;要求数组变量作为参数的语句或函数。 (2)也有一些指令使用方法不同。返回字符串以一个美元符($)结束的wordbasic函数的关键字必须括在方括号中
20、。例如,在wordbasic宏中的getbookmark$()语句:mark$=getbookmark$("address"),若用vb调用,必须这样写mark$=wordobj.ge-tbookmark$("address")。选择一个命令按钮用“true”,不选择用“false” 2.3 对ole自动化的说明 word可以为ole自动化给另一个应用提供对象,但是它不能使用ole自动化访问其它应用中的对象。例如:vb和excel可以使用ole自动化访问word,但是word不能使用ole自动化访问它们。 3 在包容器中嵌入word对象 在vb中,要访问
21、在包容器中嵌入的word对象,首先要在项目中插入对象。做法如下:在窗体中插入ole控件,对象类型选择“microsoftword图片”或“microsoft word文档”,再按“确定”。 然后用object属性访问文档或图片,并使用wordbasic语句和函数作用于它。嵌入的对象必须在可被访问之前被激活,可以使用action属性激活ole控件。例如,使用下面指令访问一个嵌入在称为ole1的ole控件中的文档: dim wordobj as object ole1.action =7 set wordobj =ole1.object.application. wordbasic 其他方面,使用
22、方法同ole自动化。使用在包容器中嵌入的word对象,word显示的窗口大小、位置与ole控件定义的大小、位置相同,而且工具栏显示位置与word脱离。这一点与ole自动化相比,是个不足。 总之,要想在microsoft visual basic中控制microsoft word,最好使用ole自动化,通过使用wordbasic指令对word进行全面控制,而且,用户使用起来与使用microsoft word一样,非常方便 _用vb如何在word指定位置上插入文字?悬赏分:150 - 解决时间:2006-12-6 10:21提问者: sxtyhjh - 见习魔法师 二级 最佳答案在vb6.0中,操
23、作word,使用它强大的查找、替换、删除、复制、翦切功能。还可以把特定字符替换成图片。有了它你就可以使用数据库中的内容或图片文件替换word文件中的特定字符。 只要把下列内容复制到写字板中,另存为setword.cls文件,然后在把它添加到工程中,就可以使用了。 version 1.0 class begin multiuse = -1 'true persistable = 0 'notpersistable databindingbehavior = 0 'vbnone datasourcebehavior = 0 'vbnone mtstransactio
24、nmode = 0 'notanmtsobject end attribute vb_name = "setword" attribute vb_globalnamespace = false attribute vb_creatable = true attribute vb_predeclaredid = false attribute vb_exposed = false private mywdapp as word.application private mysel as object '属性值的模块变量 private c_templatedoc
25、 as string private c_newdoc as string private c_picfile as string private c_errmsg as integer public event haveerror() attribute haveerror.vb_description = "出错时激发此事件.出错代码为errmsg属性" '* 'errmsg代码:1word没有安装 2 - 缺少参数 3 - 没权限写文件 ' 4 - 文件不存在 ' '* public function replacepic(fi
26、ndstr as string, optional time as integer = 0) as integer attribute replacepic.vb_description = "查找findstr,并替换为picfile所指向的图片文件,替换次数由time参数确定,为0时,替换所有" '* ' 从word.range对象mysel中查找所有findstr,并替换为picfile图像 ' 替换次数由time参数确定,为0时,替换所有 '* if len(c_picfile) = 0 then c_errmsg = 2 exit
27、function end if dim i as integer dim findtxt as boolean mysel.find.clearformatting mysel.find.replacement.clearformatting with mysel.find .text = findstr .replacement.text = "" .forward = true .wrap = wdfindcontinue .format = false .matchcase = false .matchwholeword = false .matchbyte = tr
28、ue .matchwildcards = false .matchsoundslike = false .matchallwordforms = false end with mysel.homekey unit:=wdstory findtxt = mysel.find.execute(replace:=true) if not findtxt then replacepic = 0 exit function end if i = 1 do while findtxt mysel.inlineshapes.addpicture filename:=c_picfile if i = time
29、 then exit do i = i + 1 mysel.homekey unit:=wdstory findtxt = mysel.find.execute(replace:=true) loop replacepic = i end function public function findthis(findstr as string) as boolean attribute findthis.vb_description = "查找findstr,如果模板中有findstr则返回true" if len(findstr) = 0 then c_errmsg = 2
30、 exit function end if mysel.find.clearformatting mysel.find.replacement.clearformatting with mysel.find .text = findstr .replacement.text = "" .forward = true .wrap = wdfindcontinue .format = false .matchcase = false .matchwholeword = false .matchbyte = true .matchwildcards = false .matchs
31、oundslike = false .matchallwordforms = false end with mysel.homekey unit:=wdstory findthis = mysel.find.execute end function public function replacechar(findstr as string, repstr as string, optional time as integer = 0) as integer attribute replacechar.vb_description = "查找findstr,并替换为repstr,替换次
32、数由time参数确定,为0时,替换所有" '* ' 从word.range对象mysel中查找findstr,并替换为repstr ' 替换次数由time参数确定,为0时,替换所有 '* dim findtxt as boolean if len(findstr) = 0 then c_errmsg = 2 raiseevent haveerror exit function end if mysel.find.clearformatting mysel.find.replacement.clearformatting with mysel.find
33、.text = findstr .replacement.text = repstr .forward = true .wrap = wdfindcontinue .format = false .matchcase = false .matchwholeword = false .matchbyte = true .matchwildcards = false .matchsoundslike = false .matchallwordforms = false end with if time > 0 then for i = 1 to time mysel.homekey unit
34、:=wdstory findtxt = mysel.find.execute(replace:=wdreplaceone) if not findtxt then exit for next if i = 1 and not findtxt then replacechar = 0 else replacechar = i end if else mysel.find.execute replace:=wdreplaceall end if end function public function getpic(picdata() as byte, filename as string) as
35、 boolean attribute getpic.vb_description = "把图像数据picdata,存为picfile指定的文件" '* ' 把图像数据picdata,存为picfile指定的文件 '* on error resume next if len(filename) = 0 then c_errmsg = 2 raiseevent haveerror exit function end if open filename for binary as #1 if err.number 0 then c_errmsg = 3 ex
36、it function end if '二进制文件用get,put存放,读取数据 put #1, , picdata close #1 c_picfile = filename getpic = true end function public sub deletetoend() attribute deletetoend.vb_description = "删除从当前位置到结尾的所有内容" mysel.endkey unit:=wdstory, extend:=wdextend mysel.delete unit:=wdcharacter, count:=1 en
37、d sub public sub moveend() attribute moveend.vb_description = "光标移动到文档结尾" '光标移动到文档结尾 mysel.endkey unit:=wdstory end sub public sub gotoline(linetime as integer) mysel.goto what:=wdgotoline, which:=wdgotofirst, count:=linetime, name:="" end sub public sub opendoc(view as boole
38、an) attribute opendoc.vb_description = "打开word文件,view确定是否显示word界面" on error resume next '* ' 打开word文件,并给全局变量mysel赋值 '* if len(c_templatedoc) = 0 then mywdapp.documents.add else mywdapp.documents.open (c_templatedoc) end if if err.number 0 then c_errmsg = 4 raiseevent haveerror
39、exit sub end if mywdapp.visible = view mywdapp.activate set mysel = mywdapp.application.selection 'mysel.select end sub public sub openword() on error resume next '* ' 打开word程序,并给全局变量mywdapp赋值 '* set mywdapp = createobject("word.application") if err.number 0 then c_errmsg =
40、 1 raiseevent haveerror exit sub end if end sub public sub viewdoc() attribute viewdoc.vb_description = "显示word程序界面" mywdapp.visible = true end sub public sub addnewpage() attribute addnewpage.vb_description = "插入分页符" mysel.insertbreak type:=wdpagebreak end sub public sub wordcut
41、() attribute wordcut.vb_description = "剪切模板所有内容到剪切板" '保存模板页面内容 mysel.wholestory mysel.cut mysel.homekey unit:=wdstory end sub public sub wordcopy() attribute wordcopy.vb_description = "拷贝模板所有内容到剪切板" mysel.wholestory mysel.copy mysel.homekey unit:=wdstory end sub public sub wo
42、rddel() mysel.wholestory mysel.delete mysel.homekey unit:=wdstory end sub public sub wordpaste() attribute wordpaste.vb_description = "拷贝剪切板内容到当前位置" '插入模块内容 mysel.paste end sub public sub closedoc() attribute closedoc.vb_description = "关闭word文件模板" '* ' 关闭word文件模本 '
43、;* on error resume next mywdapp.activedocument.close false if err.number 0 then c_errmsg = 3 exit sub end if end sub public sub quitword() '* ' 关闭word程序 '* on error resume next mywdapp.quit if err.number 0 then c_errmsg = 3 exit sub end if end sub public sub savetodoc() attribute savetod
44、oc.vb_description = "保存当前文档为filename指定文件" on error resume next '并另存为文件filename if len(c_newdoc) = 0 then c_errmsg = 2 raiseevent haveerror exit sub end if mywdapp.activedocument.saveas (c_newdoc) if err.number 0 then c_errmsg = 3 raiseevent haveerror exit sub end if end sub public prop
45、erty get templatedoc() as string attribute templatedoc.vb_description = "模板文件名." templatedoc = c_templatedoc end property public property let templatedoc(byval vnewvalue as string) c_templatedoc = vnewvalue end property public property get newdoc() as string attribute newdoc.vb_description
46、 = "执行closedoc方法时,将模板文件另存为此文件名指定的新文件.如果不指定,在执行closedoc方法时,将产生一个错误" newdoc = c_newdoc end property public property let newdoc(byval vnewvalue as string) c_newdoc = vnewvalue end property public property get picfile() as string attribute picfile.vb_description = "图像文件名" picfile = c
47、_picfile end property public property let picfile(byval vnewvalue as string) c_picfile = vnewvalue end property public property get errmsg() as integer attribute errmsg.vb_description = "错误信息.errmsg代码: 1-word没有安装 2-缺少参数 3-没权限写文件 4-文件不存在" errmsg = c_errmsg end property _用vb调用word悬赏分:10 - 解决时间:2008-5-25 19:02我在word里写了点东西,然后想用vb调用它,请高手指点,怎么调用这个文件,假设为help.doc 请给出原程序。问题补充:你的代码没有问题,可不是我想要的,运行的结果只是显示了word内的部分内容,我
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年机器人技术应用与管理试卷及答案
- 2025年全国基层产科医师培训项目专业理论考核历年参考题库含答案
- 2025年注册验船师资格考试(A级船舶检验专业实务)经典试题及答案一
- 2025年注册验船师资格考试(A级船舶检验专业案例分析)全真冲刺试题及答案二
- 北京市门头沟区2024-2025学年八年级上学期期末考试道德与法制试题及答案
- 北京市门头沟区2023-2024学年七年级下学期第一次月考道德与法制考试题目及答案
- 2025年陵园服务礼仪与沟通技巧面试题
- 2025年生态修复师中级考试模拟题答案
- 2025年环境工程师专业能力评估模拟题及解析
- 2025年银行信贷审批员招录笔试题目及专业解答公开
- 2025 年小升初临汾市初一新生分班考试数学试卷(带答案解析)-(人教版)
- 2024年山西交通控股集团有限公司招聘真题
- 2025年妊娠期糖尿病护理查房记录模板范文
- 2025-2026学年西师大版(2024)小学数学二年级上册教学计划及进度表
- 2025年传动部件行业当前发展趋势与投资机遇洞察报告
- 基孔肯雅热预防宣传课件
- 2025-2030中国海水淡化技术经济性分析与政策补贴机制报告
- 福建卷(未来总是甜的)-2025年中考语文作文题解读
- 学校学生一日常规管理细则(2025年修订)
- 【语文】小学四年级下册期末质量模拟试题测试卷
- 2025年甘南事业单位考试笔试试题
评论
0/150
提交评论