生活随笔
收集整理的這篇文章主要介紹了
word——VBA编程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近需要批量操作一些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 ThenSet worddoc = Documents.Open(MyDir & "\" & FileName)worddoc.ActivateCall my '調用宏,換成你自己宏的名字worddoc.Close TrueFileName = Dir()End If
Loop
Set worddoc = Nothing
End SubSub my()Selection.WholeStory '全選Options.DefaultHighlightColorIndex = wdNoHighlightSelection.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 = TrueEnd With '選中文檔中文字'檢測:'Selection.Find.Execute '選中執行Selection.MoveRight Unit:=wdCharacter, Count:=1 '移動光標一格strBookmark = "PO_jc"ActiveDocument.Bookmarks.Add Name:=strBookmark, Range:=Selection.RangeWith Selection.Find.Text = " 年 月 日 ".Replacement.Text = "".Forward = True.Wrap = wdFindContinue.Format = False.MatchCase = True.MatchWholeWord = False.MatchByte = False.MatchAllWordForms = False.MatchSoundsLike = False.MatchWildcards = False.MatchFuzzy = TrueEnd With '找到 年 月 日Selection.Find.ExecuteSelection.Delete'刪除選中內容End Sub
總結
以上是生活随笔為你收集整理的word——VBA编程的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。