word VBA程式設計

2021-09-26 10:19:34 字數 2279 閱讀 9599

最近需要批量操作一些word檔案,大約四十幾個檔案把。乙個乙個手動操作真的太low了,所以研究了一下word的巨集,vba編寫**,批量操作。

操作就是全選word內容,給整體加乙個書籤po_table。

選中文件中檢測二字,加書籤po_jc。

選中 年 月 日,刪除該選中內容。

sub 批量操作word()

dim path as string

dim filename as string

dim worddoc as document

dim mydir as string

mydir = "c:\users\csy\documents\tencent files\409023706\filerecv\平台需要的\平台需要的" '資料夾路徑根據需要自己修改,需要處理的檔案都放該資料夾內

filename = dir(mydir & "\*.docx*", vbnormal)

do until filename = ""

if filename <> thisdocument.name then

set worddoc = documents.open(mydir & "\" & filename)

worddoc.activate

call my '呼叫巨集,換成你自己巨集的名字

worddoc.close true

filename = dir()

end if

loop

set worddoc = nothing

end sub

sub my()

selection.wholestory '全選

options.defaulthighlightcolorindex = wdnohighlight

selection.range.highlightcolorindex = wdnohighlight '背景色設無

selection.font.color = vbblack '字型顏色設黑

dim strbookmark as string '宣告

strbookmark = "po_table" '賦值

activedocument.bookmarks.add name:=strbookmark, range:=selection.range '給選中區域設定書籤

with selection.find

.text = "檢測:"

.replacement.text = ""

.forward = true

.wrap = wdfindcontinue

.format = false

.matchcase = true

.matchwholeword = false

.matchbyte = false

.matchallwordforms = false

.matchsoundslike = false

.matchwildcards = false

.matchfuzzy = true

end with '選中文件中文字'檢測:'

selection.find.execute '選中執行

selection.moveright unit:=wdcharacter, count:=1 '移動游標一格

strbookmark = "po_jc"

activedocument.bookmarks.add name:=strbookmark, range:=selection.range

with selection.find

.text = " 年 月 日 "

.replacement.text = ""

.forward = true

.wrap = wdfindcontinue

.format = false

.matchcase = true

.matchwholeword = false

.matchbyte = false

.matchallwordforms = false

.matchsoundslike = false

.matchwildcards = false

.matchfuzzy = true

end with '找到 年 月 日

selection.find.execute

selection.delete'刪除選中內容

end sub

Word Vba技巧總結

確定文字是否被選定 if selection.type wdselectionip then msgbox nothing is selected word中選擇單元格 selection.selectcell 移動到選定內容之後 selection.collapse direction wdcol...

本輯為WordVBA語句

本輯為wordvba語句,為保持連續,仍源用原名稱並順延編號。vba語句集的特點是 一句vba 後面配有 功能簡要的說明或解釋。每輯100句,盡可能收錄所有在程式中所要用到的 documents document物件 paragraphs paragraph物件 353 selection.para...

利用Word VBA製作選擇題

word中的vba功能可以使word文件具有互動功能,在學校教學中,教師就可以利用word vba功能在word文件中製作具有互動功能的選擇題。以使用word2003製作雙項選擇題word文件為例,操作步驟如下所述 第 1步,新建乙個空白word文件,輸入選擇題的題目部分,如 一台完整的計算機系統包...