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 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 = ""
.Replacement.Text = ""
.Forward = True
.
Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False        .MatchByte = False
.MatchAllWordForms = False        .MatchSoundsLike = False        .MatchWildcards = False
.MatchFuzzy = True
vba编程免费教程End With '到年⽉⽇
Selection.Find.Execute
Selection.Delete'删除选中内容End Sub

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。