Word利用VBA宏批量设置图片大小

2025-04-25
#Windows #Experiences
  1. 打开 Word 的“开发工具”选项卡:文件 → 选项 → 自定义功能区 → 勾选右边的“开发工具”。

  2. 插入 VBA 宏代码:点击“开发工具”→“Visual Basic”;在左侧找到“本机文档(ThisDocument)”,双击;粘贴以下代码:

Sub ResizeAllPictures()
    Dim iShape As InlineShape
    Dim shape As Shape
    Dim targetWidth As Single
    Dim targetHeight As Single

    ' 设置目标宽度和高度(单位:磅,1 厘米 ≈ 28.35 磅)
    targetWidth = CentimetersToPoints(10)   ' 例如:10 厘米宽
    targetHeight = CentimetersToPoints(12) ' 例如:12 厘米高

    ' 调整内嵌图片(与文字在一行的)
    For Each iShape In ActiveDocument.InlineShapes
        If iShape.Type = wdInlineShapePicture Then
            iShape.LockAspectRatio = msoFalse
            iShape.Width = targetWidth
            iShape.Height = targetHeight
        End If
    Next iShape

    ' 调整浮动图片(可自由拖动的)
    For Each shape In ActiveDocument.Shapes
        If shape.Type = msoPicture Then
            shape.LockAspectRatio = msoFalse
            shape.Width = targetWidth
            shape.Height = targetHeight
        End If
    Next shape

    MsgBox "所有图片已批量调整大小!"
End Sub
  1. 点击“运行”按钮(或按 F5)