萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 辦公室軟體 >  >> Word 教學 >> Word 使用技巧

Word 批量修改圖片大小與縮放比例:巨集腳本一次搞定所有圖片

Word 文檔包含大量圖片需統一調整尺寸或按比例縮放?逐張手動調整極其耗時。使用 VBA 巨集腳本可一次處理文檔中所有圖片(含內嵌型 InlineShapes 與浮動型 Shapes),支援固定尺寸與比例縮放兩種模式,並可選擇性加入置中對齊。

前置知識:單位換算

Word 內部圖片尺寸單位為 點,1 cm ≈ 28.35 點。腳本中數值單位為點,請依需求換算(如 400 點 ≈ 14.1 cm)。

模式一:固定長寬(所有圖片統一尺寸)

Sub SetPicFixedSize()
    Dim n As Long
    On Error Resume Next
    ' 處理內嵌型圖片
    For n = 1 To ActiveDocument.InlineShapes.Count
        ActiveDocument.InlineShapes(n).Height = 400 ' 高度 400 點
        ActiveDocument.InlineShapes(n).Width = 300  ' 寬度 300 點
    Next n
    ' 處理浮動型圖片/形狀
    For n = 1 To ActiveDocument.Shapes.Count
        ActiveDocument.Shapes(n).Height = 400
        ActiveDocument.Shapes(n).Width = 300
    Next n
End Sub

模式二:按比例縮放(保持原始長寬比)

Sub SetPicScaleRatio()
    Dim n As Long, 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  ' 放大 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

執行方法

  1. 按 Alt+F11 開啟 VBE,或「檢視」→「巨集」→「檢視巨集」
  2. 點選「建立」,貼上上述程式碼,儲存(Ctrl+S)
  3. 返回 Word,Alt+F8 選擇巨集名稱 →「執行」

重要提醒

  • Shapes 集合包含:圖片、自選圖形、任意多邊形、OLE 物件、ActiveX 控制項等,不僅限圖片,執行前請確認範圍
  • 建議先備份文檔,巨集操作不可復原(除非關閉不存檔)
  • 圖片數量大時執行需數秒至數分鐘,請耐心等待
  • 若需針對特定圖片類型處理,可在迴圈加入型別判斷條件
copyright © 萬盛學電腦網 all rights reserved