精品文獻(xiàn)vb操作word_第1頁
精品文獻(xiàn)vb操作word_第2頁
精品文獻(xiàn)vb操作word_第3頁
精品文獻(xiàn)vb操作word_第4頁
精品文獻(xiàn)vb操作word_第5頁
已閱讀5頁,還剩20頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、追加500分求教 vb操作word問題懸賞分:200 - 解決時(shí)間:2007-7-28 11:42說明,用vb操作word,以下源碼第一次操作完全正常,正常打開,正常替換,正常退出。進(jìn)程中并沒有留下windword的進(jìn)程,但第二次繼續(xù)操作時(shí)就出問題,運(yùn)行到replaceword()就出現(xiàn)462錯(cuò)誤,關(guān)閉程序重新開始又正常。請指教出錯(cuò)及解決原因,追加到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 =替換關(guān)鍵字= function replaceword(searchstr, replacestr) '全部替換函數(shù) 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 '清除文件實(shí)例 set wordapp = nothing '清除word實(shí)例 end function問題補(bǔ)充:根據(jù)小fisher的答案,已

6、經(jīng)解決問題,另外在saveasword過程中,changefileopendirectory diskstr更改為changefileopendirectory diskstr ,activedocument.saveas更改為worddoc.saveas,再加上原先小fisher提到要更改的地方,已經(jīng)完美解決,謝謝! ps:由于百度只能2次提高懸賞,每次50分,所以現(xiàn)在只有200分,未能對現(xiàn)500分的諾言,所以只有另開貼來加送300分!提問者: 有野問 - 經(jīng)理 五級(jí) 最佳答案1) function replaceword(searchstr, replacestr) '全部替換函數(shù)

7、 selection.find.clearformatting selection.find.replacement.clearformatting with selection.find . 這個(gè)函數(shù)過程有錯(cuò)誤!因?yàn)閟election是word的對象而不是vb的對象,所以不能在vb中直接引用,必須用wordapp.selection替換掉selection才行! 這段代碼在word vba中調(diào)試不會(huì)出問題,但移植到vb中時(shí)要注意。 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兩個(gè)變量將會(huì)是未初始化的variant類型。 3)function saveasword(diskstr, namestr)中的application須使用wordapp替換,道理同1)。_-網(wǎng)上說 在visual basic中創(chuàng)建word文檔使用語句 dim newdoc a

9、s word.document set newdoc = new word.document 但是vb會(huì)提示上述語句 “用戶定義類型未定義” 另一種創(chuàng)建的方法是 dim msword as object set msword = createobject("word.basic") msword.appshow msword.filenewdefault 可以創(chuàng)建 但是下述語句沒辦法使用(即對該文檔的一些操作) with msword .content.font.name = "宋體" .content.font.size = 12 .content.p

10、aragraphs.linespacing = 15.5 end with 請教各位高手指點(diǎn),我想在vb中創(chuàng)建一個(gè)word文檔,并將程序計(jì)算的一堆結(jié)果,在該文檔中按一定的格式打印出來。 另,我在word中錄制了一段宏,請問在vb中如何調(diào)用,要求代碼 sub zz() activedocument.tables.add range:=selection.range, numrows:=6, numcolumns:= _ 6, defaulttablebehavior:=wdword9tablebehavior, autofitbehavior:= _ wdautofitfixed with se

11、lection.tables(1) if .style "網(wǎng)格型" then .style = "網(wǎng)格型" 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 - 助理 三級(jí) 最佳答案將 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時(shí),在程序讀完,為什么還有關(guān)聯(lián),怎樣給她關(guān)聯(lián)除啦懸賞分:0 - 解決時(shí)間:2008-10-23 10:06用vb實(shí)現(xiàn)讀word程序的操作! 也就是說

14、當(dāng)我運(yùn)行vb程序時(shí)讀啦一次word 第二次再執(zhí)行時(shí)還是第一次的結(jié)果! 例如執(zhí)行下面這個(gè)語句時(shí): if newdoc.paragraphs(1).range.font.name = "新 宋 體" then s = s + 0.5 x = msgbox("s=" + str$(s), 64, "字體為1.5") 執(zhí)行第一次之后,再改程序,執(zhí)行時(shí),總是提示word在另一個(gè)地方已經(jīng)打開,實(shí)際上沒有打開,我想這是由于關(guān)聯(lián)的問題!提問者: lcs6678 - 助理 二級(jí) 最佳答案在任務(wù)管理器下的進(jìn)程下看看,肯定打開了。 不知道你是用什么方法定義

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") 用以下語句關(guān)閉 b.close a.quit _在vb如何存取word格式的文件懸賞分:30 - 解決時(shí)間:2006-9-12 08:30我想把用vb計(jì)算的一堆結(jié)果打印在word里面,并且有固定格式的簡單排版,最好能在word中打印一些表格,請問用什么樣的函數(shù)或者對象實(shí)踐?希望高手幫忙

16、,要是能加一點(diǎn)說明,更加感激不盡提問者: zhaizhaiya - 助理 二級(jí) 最佳答案我?guī)湍闼阉鞯搅藘煞N方法 雖然我自己也不懂 但是也許你會(huì)懂吧 通過vba進(jìn)行word調(diào)用,要替換word中固定位置的值,可以事先在文檔中定義標(biāo)簽,然后修改標(biāo)簽的內(nèi)容即可,不用查找。 vb中用vba調(diào)用word的方法: 菜單-工程-引用->microsoft word 9.0 object library (后面的數(shù)字為版本號(hào)) dim wdapp as new word.application dim docapp as new word.document 具體用怎么用標(biāo)簽替換,可以到word中錄制宏看

17、看就知道了,宏可以直接在vb中通過vba執(zhí)行。 方法2 使用vb編程時(shí),有時(shí)需要調(diào)用microsoft word對文字進(jìn)行編輯、排版及輸出。為實(shí)現(xiàn)這種調(diào)用,可以使用shell函數(shù)、ole自動(dòng)化、在包容器中嵌入word對象等方法。經(jīng)過試用和比較,總結(jié)出了這幾種方法的各自特點(diǎn)。 1 使用shell函數(shù)直接調(diào)用 語法:shell (pathname,windowstyle).pathname是指要執(zhí)行的程序的名字和任何必須的參數(shù)或命令行開關(guān),可以包括目錄和驅(qū)動(dòng)器名;windowstyle是執(zhí)行程序的窗口風(fēng)格的數(shù)字。 使用shell調(diào)用word比較簡單,編程量小,但必須明確指定word所在路徑,這不利

18、于移植,而且,不能對word進(jìn)行控制,不利于程序和word之間的數(shù)據(jù)交換。 2 使用ole自動(dòng)化控制microsoft word 2.1 使用方法 (1)word為ole自動(dòng)化提供一種稱為“basic”的對象,要在vb中控制word ,首先要定義一個(gè)引用word中“basic”對象的對象變量:dim wordobj as object (2)將word 中的“basic”對象賦給該對象:set wordobj=createobject("word.basic") (3)可以使用大多數(shù)wordbasic語句和函數(shù)控制word或word文檔,使用方法和在word宏中使用word

19、basic指令的方法基本相同。 (4)關(guān)閉word:set wordobj =nothing。 注意:“basic”對象不支持關(guān)閉它自己的一個(gè)方法。即若在ole自動(dòng)化中關(guān)閉了word,則對象被置為nothing,便不能再對對象進(jìn)行操作,程序出錯(cuò)。 2.2 vb指令與wordbasic指令的差異 (1)有一些語句和函數(shù)不能使用,包括:控制結(jié)構(gòu),如whilewend和ifthenelse;聲明語句,如dim;定制對話框相關(guān)的語句:fileexit語句;要求數(shù)組變量作為參數(shù)的語句或函數(shù)。 (2)也有一些指令使用方法不同。返回字符串以一個(gè)美元符($)結(jié)束的wordbasic函數(shù)的關(guān)鍵字必須括在方括號(hào)中

20、。例如,在wordbasic宏中的getbookmark$()語句:mark$=getbookmark$("address"),若用vb調(diào)用,必須這樣寫mark$=wordobj.ge-tbookmark$("address")。選擇一個(gè)命令按鈕用“true”,不選擇用“false” 2.3 對ole自動(dòng)化的說明 word可以為ole自動(dòng)化給另一個(gè)應(yīng)用提供對象,但是它不能使用ole自動(dòng)化訪問其它應(yīng)用中的對象。例如:vb和excel可以使用ole自動(dòng)化訪問word,但是word不能使用ole自動(dòng)化訪問它們。 3 在包容器中嵌入word對象 在vb中,要訪問

21、在包容器中嵌入的word對象,首先要在項(xiàng)目中插入對象。做法如下:在窗體中插入ole控件,對象類型選擇“microsoftword圖片”或“microsoft word文檔”,再按“確定”。 然后用object屬性訪問文檔或圖片,并使用wordbasic語句和函數(shù)作用于它。嵌入的對象必須在可被訪問之前被激活,可以使用action屬性激活ole控件。例如,使用下面指令訪問一個(gè)嵌入在稱為ole1的ole控件中的文檔: dim wordobj as object ole1.action =7 set wordobj =ole1.object.application. wordbasic 其他方面,使用

22、方法同ole自動(dòng)化。使用在包容器中嵌入的word對象,word顯示的窗口大小、位置與ole控件定義的大小、位置相同,而且工具欄顯示位置與word脫離。這一點(diǎn)與ole自動(dòng)化相比,是個(gè)不足。 總之,要想在microsoft visual basic中控制microsoft word,最好使用ole自動(dòng)化,通過使用wordbasic指令對word進(jìn)行全面控制,而且,用戶使用起來與使用microsoft word一樣,非常方便 _用vb如何在word指定位置上插入文字?懸賞分:150 - 解決時(shí)間:2006-12-6 10:21提問者: sxtyhjh - 見習(xí)魔法師 二級(jí) 最佳答案在vb6.0中,操

23、作word,使用它強(qiáng)大的查找、替換、刪除、復(fù)制、翦切功能。還可以把特定字符替換成圖片。有了它你就可以使用數(shù)據(jù)庫中的內(nèi)容或圖片文件替換word文件中的特定字符。 只要把下列內(nèi)容復(fù)制到寫字板中,另存為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 = "出錯(cuò)時(shí)激發(fā)此事件.出錯(cuò)代碼為errmsg屬性" '* 'errmsg代碼:1word沒有安裝 2 - 缺少參數(shù) 3 - 沒權(quán)限寫文件 ' 4 - 文件不存在 ' '* public function replacepic(fi

26、ndstr as string, optional time as integer = 0) as integer attribute replacepic.vb_description = "查找findstr,并替換為picfile所指向的圖片文件,替換次數(shù)由time參數(shù)確定,為0時(shí),替換所有" '* ' 從word.range對象mysel中查找所有findstr,并替換為picfile圖像 ' 替換次數(shù)由time參數(shù)確定,為0時(shí),替換所有 '* 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、數(shù)由time參數(shù)確定,為0時(shí),替換所有" '* ' 從word.range對象mysel中查找findstr,并替換為repstr ' 替換次數(shù)由time參數(shù)確定,為0時(shí),替換所有 '* 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 = "把圖像數(shù)據(jù)picdata,存為picfile指定的文件" '* ' 把圖像數(shù)據(jù)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 '二進(jìn)制文件用get,put存放,讀取數(shù)據(jù) put #1, , picdata close #1 c_picfile = filename getpic = true end function public sub deletetoend() attribute deletetoend.vb_description = "刪除從當(dāng)前位置到結(jié)尾的所有內(nèi)容" mysel.endkey unit:=wdstory, extend:=wdextend mysel.delete unit:=wdcharacter, count:=1 en

37、d sub public sub moveend() attribute moveend.vb_description = "光標(biāo)移動(dòng)到文檔結(jié)尾" '光標(biāo)移動(dòng)到文檔結(jié)尾 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 = "剪切模板所有內(nèi)容到剪切板" '保存模板頁面內(nèi)容 mysel.wholestory mysel.cut mysel.homekey unit:=wdstory end sub public sub wordcopy() attribute wordcopy.vb_description = "拷貝模板所有內(nèi)容到剪切板" 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 = "拷貝剪切板內(nèi)容到當(dāng)前位置" '插入模塊內(nèi)容 mysel.paste end sub public sub closedoc() attribute closedoc.vb_description = "關(guān)閉word文件模板" '* ' 關(guān)閉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() '* ' 關(guān)閉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 = "保存當(dāng)前文檔為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、 = "執(zhí)行closedoc方法時(shí),將模板文件另存為此文件名指定的新文件.如果不指定,在執(zhí)行closedoc方法時(shí),將產(chǎn)生一個(gè)錯(cuò)誤" 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 = "錯(cuò)誤信息.errmsg代碼: 1-word沒有安裝 2-缺少參數(shù) 3-沒權(quán)限寫文件 4-文件不存在" errmsg = c_errmsg end property _用vb調(diào)用word懸賞分:10 - 解決時(shí)間:2008-5-25 19:02我在word里寫了點(diǎn)東西,然后想用vb調(diào)用它,請高手指點(diǎn),怎么調(diào)用這個(gè)文件,假設(shè)為help.doc 請給出原程序。問題補(bǔ)充:你的代碼沒有問題,可不是我想要的,運(yùn)行的結(jié)果只是顯示了word內(nèi)的部分內(nèi)容,我

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論