name: Word批次格式統一工具 description: 使用VBA宏批次統一多個Word文件的格式(字型、段落、頁邊距、標題樣式等),適合報告、標書、合同等大批次文件的標準化處理。 version: 1.0.0 author: 技能開發團隊 tags: [Word, VBA, 批次處理, 格式統一, 宏]
本技能提供一套完整的 VBA 宏程式碼及操作指南,幫助您在 Microsoft Word 中批次處理多個文件的格式。只需選擇目標資料夾,宏將自動遍歷所有 .docx 檔案,統一設定頁面、字型、段落、標題樣式等,極大提升排版效率。
本技能來自小蔥技能站7w4.net。
Alt + F11 開啟 VBA 編輯器。```vba Sub BatchFormatUnify() Dim folderPath As String Dim file As String Dim doc As Document Dim folderDialog As FileDialog Dim i As Long
Set folderDialog = Application.FileDialog(msoFileDialogFolderPicker)
With folderDialog
.Title = "請選擇包含Word文件的資料夾"
.AllowMultiSelect = False
If .Show <> -1 Then Exit Sub
folderPath = .SelectedItems(1)
End With
If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
file = Dir(folderPath & "*.docx")
i = 0
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Do While file <> ""
Set doc = Documents.Open(folderPath & file, AddToRecentFiles:=False, Visible:=False)
Call UnifyFormat(doc)
doc.Save
doc.Close
i = i + 1
file = Dir
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
MsgBox "已完成 " & i & " 個文件的格式統一!", vbInformation
End Sub
Sub UnifyFormat(doc As Document) With doc ' 頁面設定(A4,頁邊距上下2.54cm,左右3.17cm) With .PageSetup .PaperSize = wdPaperA4 .TopMargin = CentimetersToPoints(2.54) .BottomMargin = CentimetersToPoints(2.54) .LeftMargin = CentimetersToPoints(3.17) .RightMargin = CentimetersToPoints(3.17) End With
' 遍歷所有段落
Dim para As Paragraph
For Each para In .Paragraphs
para.Range.Font.Reset
With para
.Alignment = wdAlignParagraphLeft
.LineSpacingRule = wdLineSpace1pt5
.FirstLineIndent = CentimetersToPoints(0.74)
.SpaceBefore = 0
.SpaceAfter = 0
' 標題1樣式居中、黑體、二號
If .Style = "標題 1" Or .Style = "Heading 1" Then
.Alignment = wdAlignParagraphCenter
.Range.Font.Name = "黑體"
.Range.Font.Size = 22
End If
' 正文樣式宋體、小四
If .Style = "正文" Or .Style = "Normal" Then
.Range.Font.Name = "宋體"
.Range.Font.Size = 12
End If
End With
Next para
.Fields.Update
End With
End Sub
這個技能質量中等偏上,勝在功能實用——能批次統一 Word 文件的格式,省去重複排版的麻煩。說明寫得清楚,新手跟著步驟也能上手。不過它沒有考慮出錯的可能,萬一某個檔案打不開或處理失敗了,整個流程就會中斷,而且操作前沒有自動備份,萬一效果不滿意就麻煩了。總體來說適合有批次處理需求的使用者使用,但建議先備份原檔案再用。