提供一個(gè)方案給樓主參考,可以批量修改多個(gè)文檔中某個(gè)指定文字的屬性:
1、啟動(dòng)Word后,鍵入Alt+F11打開VBA編輯窗口;
2、選擇菜單“插入->模塊”,然后在代碼編輯區(qū)中粘貼如下代碼:
Option Explicit
Const g_strRootPath = c:\Docs\ ' 指定存放所有文件的目錄,可以有子目錄
Const g_strTextToFind = 茶 ' 需要批量查找修改格式的文字內(nèi)容
Dim g_oTargetFont As New Font
' 主函數(shù)
Sub Main()
Dim fso, oFolder
' 設(shè)置需要修改的字體屬性
g_oTargetFont.Size = 18 ' 字號(hào)
g_oTargetFont.Color = wdColorRed ' 顏色
g_oTargetFont.Bold = True ' 是否加粗(True加粗,F(xiàn)alse正常)
g_oTargetFont.Italic = True ' 是否斜體(True斜體,F(xiàn)alse正常)
g_oTargetFont.Underline = wdUnderlineDash ' 下劃線風(fēng)格
'... 設(shè)置其他字體屬性
Set fso = CreateObject(Scripting.FileSystemObject)
Set oFolder = fso.GetFolder(g_strRootPath)
ChangeFontStyleForFilesUnderFolder fso, oFolder
MsgBox 完成!
End Sub
' 修改指定文件夾(遞歸)下面的所有Word文件中指定文字的格式
Sub ChangeFontStyleForFilesUnderFolder(fso, oFolder)
Dim oSubFolder, oFile
For Each oSubFolder In oFolder.SubFolders
ChangeFontStyleForFilesUnderFolder fso, oSubFolder
Next
For Each oFile In oFolder.Files
Documents.Open oFile.Path
ChangeFontStyleForActiveDocument
ActiveDocument.Close True
Next
End Sub
' 修改當(dāng)前打開文檔里面所有指定文字的格式
Sub ChangeFontStyleForActiveDocument()
Selection.StartOf wdStory
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = g_strTextToFind
.Replacement.Text = ^&
.Replacement.Font = g_oTargetFont
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
3、修改代碼開始處的兩個(gè)參數(shù):存放所有需要批量修改文檔的根目錄(下面可以包含子目錄,比如“C:\Docs”),需要修改的文字(比如“茶”字,也可以是一個(gè)詞或者句子);
4、修改“主函數(shù)”里面想要設(shè)置的字體屬性,如字號(hào)、顏色、加粗、斜體、下劃線,等等;
4、鍵入F5運(yùn)行,直到看到“完成!”。
5、檢查各個(gè)文檔里面指定的文字是否已經(jīng)被設(shè)置成了指定的格式。
