文件中幾十張圖片需統一尺寸或按比例縮放,逐張調整極耗時。利用 Word VBA 巨集,可針對「內嵌圖片」與「浮動圖片」兩類物件批量設定固定寬高或等比縮放,並可同步設定置中對齊。本文提供完整巨集碼與操作步驟。
巨集需分別處理兩類集合。
SetPicFixedSize,點擊「建立」。
圖 1:建立新巨集
Sub SetPicFixedSize() ' 設定圖片固定大小
Dim n As Long
On Error Resume Next ' 忽略錯誤 (如群組物件無法直接設定尺寸)
' 處理內嵌圖片
For n = 1 To ActiveDocument.InlineShapes.Count
ActiveDocument.InlineShapes(n).Height = 400 ' 高度 400 px
ActiveDocument.InlineShapes(n).Width = 300 ' 寬度 300 px
Next n
' 處理浮動圖片/形狀
For n = 1 To ActiveDocument.Shapes.Count
ActiveDocument.Shapes(n).Height = 400
ActiveDocument.Shapes(n).Width = 300
Next n
End Sub

圖 2:巨集程式碼
SetPicFixedSize 點擊「執行」。圖片較多時需等待數秒。Sub SetPicScale() ' 設定圖片按比例縮放
Dim n As Long
Dim picWidth As Single, picHeight As Single
On Error Resume Next
' 內嵌圖片
For n = 1 To ActiveDocument.InlineShapes.Count
picHeight = ActiveDocument.InlineShapes(n).Height
picWidth = ActiveDocument.InlineShapes(n).Width
ActiveDocument.InlineShapes(n).Height = picHeight * 1.1
ActiveDocument.InlineShapes(n).Width = picWidth * 1.1
Next n
' 浮動圖片/形狀
For n = 1 To ActiveDocument.Shapes.Count
picHeight = ActiveDocument.Shapes(n).Height
picWidth = ActiveDocument.Shapes(n).Width
ActiveDocument.Shapes(n).Height = picHeight * 1.1
ActiveDocument.Shapes(n).Width = picWidth * 1.1
Next n
End Sub
在迴圈中加入:
ActiveDocument.InlineShapes(n).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
' 靠左:wdAlignParagraphLeft
' 靠右:wdAlignParagraphRight
Type = msoPicture)。