利用VBA將word文件中的所有圖片更改尺寸

利用VBA將word文件中的所有圖片更改尺寸

利用VBA將word文件中的所有圖片更改尺寸。

Sub ResizeImage()

    Dim shape As InlineShape

    For Each shape In Selection.InlineShapes
        
        'This doesn't work
        'shape.LockAspectRatio = msoTrue

        shape.ScaleWidth = 50
        shape.ScaleHeight = 50
    Next
End Sub

參考這個網站

如果只要更改大於某個尺寸的圖片,用下面的程式碼

Sub ResizePicturesMaxWidth()
Dim iShape As InlineShape
Dim maxWidth As Single

' Maximum width = 20.5 cm (converted to points)
maxWidth = CentimetersToPoints(20.5)

' Loop through all InlineShapes
For Each iShape In ActiveDocument.InlineShapes
With iShape
' Keep proportions
.LockAspectRatio = msoTrue

' Only resize if the image is wider than 20.5 cm
If .Width > maxWidth Then
.Width = maxWidth
End If
End With
Next iShape
End Sub

No comments:

Post a Comment