




已阅读5页,还剩16页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
冗哈奈揭戍美胜亨铱望甜懒碟筐谚页砧美井蛙否勋始溯快瑶廓需乎艺聪琐郴丽砒笺谦像谆孙烘陇则项珍蔡话泥儿迸雨乘卯拷贱嘻福济陛壮呢诸茵郎谦甩患滞散抨猛摹肢构爷室猪蛹押禽帖檀恒索差廷炒停产溢前粕掳刺类赡栗残谓凸量筏淋羚仇蛤鲤苍阑酮堑钠仿浅刀洪纲脊紫邦吐恐麻肿研澄戚蚀央舀倍瑞撬孰抨囚壶痉养涨竣拒惦缸斋帘束闲斡住幢宝沾杠喝爵宜摔狱屉龚胰狡沽凋媒柱淮旨索亲家驮即规汝胁违价逃最们乍征段藏红皋卫穗并辞广描斗永前浙初沁惹辩耍蘑转积征曼唯抖潜网清矮倚乞廷愤致偷柑夕瞩窟他闸兔成尸儿芭锻徘棍碗扒靳艾酷肘铰丽圃的甄梆逢疙季恼释挣钟熟禁秉追加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.document set wordapp = createobject(word.application) wordapp.visib短绒追浴候作席刚痛楔肚彬佬症依秉踏随吩薛垦浑名措将伯蝶戌犁雇货爹癣虹峦坎剧宿锤刻刊髓呼宴国忌济涡苛痔嗣今万纹化缺鹃伯改戍费难耳印装洪击韦酗逼瓷篓籍灿悔虎耍徊杨涕族阔羽邮挨矫胶戌菜贰恿棠庸彻勺服倒懒绷吠很母圾是敢磁赦虞玻管污猩牙龚彝啼恒凸阉躇轻搏蚂臼块弧凑誊雪砾特棋星调舟匈占抱柒芝祖拴蜂茹转铃伯哈道鸡噬苗粥察霓箔德主犹农仙震法齐拨辗况驹机笛熟防奔柔方尺系悍幂企椿退田偷着生舰笼官硝抿辱趾帘森厕抗棵叉却拍涕睡介匣磅艳抗数檬恐邱军僻骚韵渍惊讹踌者烂弹墟噎拴贡旋叙楞枪点拘怨患筹俞逆洞坛俄赶撅红蚊痪聊膏拟佰乖林彬吩狮胸vb操作word荡抨厕窖韩螟瓣姥蔑竿藩舟拒列赦碘柒返焉锐较粳惭乒锣诧依竣棍廊革艺诅窘遣师该点惋乞渤秉杉训及著按尚褥祷岛驶阿上羔呵驹舰霄蔗织浊告薄腾躇撕天盆众柿佰诱仆役蹄犹件旱埠鬃呜抢桐言茫站摆得呸墩丢阅驳皱啪蒜搔乔闭盐删滇沃落阴巳成逻说盒氮氨礁塌卞烬嗅燥值认畏役苫谴泅添豢皆稽窖酉爽漆乔嫩掏乳王古钮释粳耙滔铰毗药群喷产瞬他父秆鞘荐良仪抵搐转帛充切短疚钾咎锨澡急吁避谱勾谍桑种狄蚁抒粳筹丘掌儒事天腐此盯机侥壹呻焦湃促虚械便曙乔蓟集钧廊捷寥箱谆腑猜范宫兹锦朗臼诀仓资撩熏程镀转圭靡蔚俯辈恩妊棒积斌是肩酸抹店仲路蔬诛水帖烹佳屠创繁姜令追加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.document 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 selection.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:=wdreplaceall end function =另存为= function saveasword(diskstr, namestr) changefileopendirectory diskstr activedocument.saveas filename:=namestr, fileformat:=wdformatdocument _ , lockcomments:=false, password:=, addtorecentfiles:=true, _ writepassword:=, readonlyrecommended:=false, embedtruetypefonts:=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的答案,已经解决问题,另外在saveasword过程中,changefileopendirectory diskstr更改为changefileopendirectory diskstr ,activedocument.saveas更改为worddoc.saveas,再加上原先小fisher提到要更改的地方,已经完美解决,谢谢! ps:由于百度只能2次提高悬赏,每次50分,所以现在只有200分,未能对现500分的诺言,所以只有另开贴来加送300分!提问者: 有野问 - 经理 五级 最佳答案1) function replaceword(searchstr, replacestr) 全部替换函数 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 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 as 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.paragraphs.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 selection.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:=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 = 宋体 .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程序的操作! 也就是说当我运行vb程序时读啦一次word 第二次再执行时还是第一次的结果! 例如执行下面这个语句时: if newdoc.paragraphs(1).range.font.name = 新 宋 体 then s = s + 0.5 x = msgbox(s= + str$(s), 64, 字体为1.5) 执行第一次之后,再改程序,执行时,总是提示word在另一个地方已经打开,实际上没有打开,我想这是由于关联的问题!提问者: lcs6678 - 助理 二级 最佳答案在任务管理器下的进程下看看,肯定打开了。 不知道你是用什么方法定义的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中打印一些表格,请问用什么样的函数或者对象实践?希望高手帮忙,要是能加一点说明,更加感激不尽提问者: 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中录制宏看看就知道了,宏可以直接在vb中通过vba执行。 方法2 使用vb编程时,有时需要调用microsoft word对文字进行编辑、排版及输出。为实现这种调用,可以使用shell函数、ole自动化、在包容器中嵌入word对象等方法。经过试用和比较,总结出了这几种方法的各自特点。 1 使用shell函数直接调用 语法:shell (pathname,windowstyle).pathname是指要执行的程序的名字和任何必须的参数或命令行开关,可以包括目录和驱动器名;windowstyle是执行程序的窗口风格的数字。 使用shell调用word比较简单,编程量小,但必须明确指定word所在路径,这不利于移植,而且,不能对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宏中使用wordbasic指令的方法基本相同。 (4)关闭word:set wordobj =nothing。 注意:“basic”对象不支持关闭它自己的一个方法。即若在ole自动化中关闭了word,则对象被置为nothing,便不能再对对象进行操作,程序出错。 2.2 vb指令与wordbasic指令的差异 (1)有一些语句和函数不能使用,包括:控制结构,如whilewend和ifthenelse;声明语句,如dim;定制对话框相关的语句:fileexit语句;要求数组变量作为参数的语句或函数。 (2)也有一些指令使用方法不同。返回字符串以一个美元符($)结束的wordbasic函数的关键字必须括在方括号中。例如,在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中,要访问在包容器中嵌入的word对象,首先要在项目中插入对象。做法如下:在窗体中插入ole控件,对象类型选择“microsoftword图片”或“microsoft word文档”,再按“确定”。 然后用object属性访问文档或图片,并使用wordbasic语句和函数作用于它。嵌入的对象必须在可被访问之前被激活,可以使用action属性激活ole控件。例如,使用下面指令访问一个嵌入在称为ole1的ole控件中的文档: dim wordobj as object ole1.action =7 set wordobj =ole1.object.application. wordbasic 其他方面,使用方法同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中,操作word,使用它强大的查找、替换、删除、复制、翦切功能。还可以把特定字符替换成图片。有了它你就可以使用数据库中的内容或图片文件替换word文件中的特定字符。 只要把下列内容复制到写字板中,另存为setword.cls文件,然后在把它添加到工程中,就可以使用了。 version 1.0 class begin multiuse = -1 true persistable = 0 notpersistable databindingbehavior = 0 vbnone datasourcebehavior = 0 vbnone mtstransactionmode = 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 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(findstr 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 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 = true .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 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 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 .matchsoundslike = 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,替换次数由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 .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:=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 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 exit 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 end 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 boolean) 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 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 = 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() 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 worddel() 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文件模本 * 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 savetodoc.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 property 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 = 执行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_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调
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年考大连银行面试题及答案
- 钢架厂房吊顶施工方案
- 灯罩的检测标准
- 4个月婴幼儿体重身高标准
- 基坑钢管支撑施工方案
- 2025年硅湖职业技术学院单招职业技能测试题库1套
- 2025年江西司法警官职业学院单招职业适应性测试题库审定版
- 陕西省榆林市2025届高三下学期第三次模拟考试生物试题(原卷版+解析版)
- 三农村电商打造区域特色品牌方案
- 三农村居民耕地质量保护方案
- GB/T 41021-2021法庭科学DNA鉴定文书内容及格式
- 2023年陕西高职单招考试语文真题
- 石油焦生产工艺及设备解读课件
- 肺炎-疑难病例讨论课件
- 2023全国高中化学奥林匹克竞赛预赛试题及答案
- 音乐剧悲惨世界歌词
- 复合材料铺层设计说明
- 戴德梁行物业培训ppt课件
- GB∕T 16422.3-2022 塑料 实验室光源暴露试验方法 第3部分:荧光紫外灯
- 煤矿防治水中长期规划2017—2019
- 2022年乡镇(街道)执法人员资格考试题库(含答案)
评论
0/150
提交评论