futrueface

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Sub AdjustImagesWidthPreserveAspectRatio()
Dim doc As Document
Dim shp As InlineShape
Dim img As Shape

Set doc = ActiveDocument ' 设置当前活动文档

' 设置图片宽度(以厘米为单位),高度将保持不变
Dim newWidthCm As Single
newWidthCm = 21.1 ' 例如,将图片宽度设置为10厘米(设置为动态获取文档宽度,一直运行会有问题,没有解决,只能设置为固定宽度)

' 将厘米转换为点数(Word中使用)
Dim newWidthPnt As Single
newWidthPnt = CentimetersToPoints(newWidthCm)

' 遍历文档中的所有内嵌形状
For Each shp In doc.InlineShapes
Set img = shp.ConvertToShape ' 转换为Shape对象以便修改

' 锁定纵横比
img.LockAspectRatio = msoTrue

' 设置环绕方式为嵌入型
img.WrapFormat.Type = wdWrapInline

' 保持原始高度,只调整宽度
img.Width = newWidthPnt ' 设置新的宽度
Next shp

MsgBox "图片宽度调整完成!"
End Sub

 

posted on 2025-01-15 00:25  futrueface  阅读(122)  评论(0)    收藏  举报