VBA_批量调整图片宽度

'版心尺寸大小(假设 Word 2003 中,A4纵向纸张,宽度已知是21厘米,左边距2.5厘米,右边距2.5厘米,所以,版心尺寸=宽度-左边距-右边距=16厘米)
    Dim Width As Single, Left As Single, Right As Single
    Width = Round(ActiveDocument.PageSetup.PageWidth / 28.35)
    Left = Round(ActiveDocument.PageSetup.LeftMargin / 28.35, 1)
    Right = Round(ActiveDocument.PageSetup.RightMargin / 28.35, 1)
    MsgBox "版心尺寸是 " & (Width - Left - Right) & " 厘米"


Sub 图片宽度批量调整()
Dim i
Dim j
Dim oldHeight
Dim oldWidth
Dim newHeight
Dim newWidth
Dim docWidth
docWidth = 15 * 28.345

On Error Resume Next
For i = 1 To ActiveDocument.InlineShapes.Count
 oldWidth = ActiveDocument.InlineShapes(i).Width
 oldHeight = ActiveDocument.InlineShapes(i).Height
 '如果长度大于内容区的长度则自动修改图片长度为内容区,图片高度按照比例压缩
 If oldWidth > docWidth Then
     newWidth = docWidth
     newHeight = newWidth * oldHeight / oldWidth
 End If
 ActiveDocument.InlineShapes(i).Height = newHeight '修改为自己需要的值
 ActiveDocument.InlineShapes(i).Width = newWidth '修改为自己需要的值

Next
For j = 1 To ActiveDocument.Shapes.Count
  oldWidth = ActiveDocument.InlineShapes(i).Width
  oldHeight = ActiveDocument.InlineShapes(i).Height
 '如果长度大于内容区的长度则自动修改图片长度为内容区,图片高度按照比例压缩
 If oldWidth > docWidth Then
     newWidth = docWidth
     newHeight = newWidth * oldHeight / oldWidth
 End If
 ActiveDocument.InlineShapes(j).Height = newHeight '修改为自己需要的值
 ActiveDocument.InlineShapes(j).Width = newWidth '修改为自己需要的值

Next

End Sub
以下是几种使用VBA批量调整图片像素大小的方法: ### 方法一:按固定宽度比例调整 ```vba Sub 批量调整图片大小() ' 循环图片集合 For Each iShape In ActiveDocument.InlineShapes ' 将高宽比还原为100% iShape.ScaleHeight = 100 iShape.ScaleWidth = 100 ' 用500除图片宽度,得出一个宽度比 myScale = 500 / iShape.Width ' 如果得出的宽度比小于1,说明图片宽度超出500 If myScale < 1 Then ' 缩小图片比例,将宽度设为500,高度按比例自动变化 iShape.ScaleHeight = myScale * 100 iShape.ScaleWidth = myScale * 100 End If Next iShape End Sub ``` 此方法会遍历文档中的所有嵌入式图片,将宽度超过500的图片按比例缩小,使宽度变为500,高度按比例自动调整 [^1]。 ### 方法二:设置固定的高度和宽度 ```vba Sub setpicsize() ' 设置图片大小 Dim n ' 图片个数 On Error Resume Next ' 忽略错误 For n = 1 To ActiveDocument.InlineShapes.Count ' InlineShapes 类型图片 ActiveDocument.InlineShapes(n).Height = 510 ' 设置图片高度为 510px ActiveDocument.InlineShapes(n).Width = 520 ' 设置图片宽度 520px Next n For n = 1 To ActiveDocument.Shapes.Count ' Shapes 类型图片 ActiveDocument.Shapes(n).Height = 510 ' 设置图片高度为 510px ActiveDocument.Shapes(n).Width = 520 ' 设置图片宽度 520px Next n End Sub ``` 该方法会将文档中所有嵌入式(InlineShapes)和浮动式(Shapes)图片的高度设置为510像素,宽度设置为520像素 [^2]。 ### 方法三:自定义高度和宽度 ```vba Sub setpicsize() Dim i Dim Height, Weight Height = 500 Weight = 200 On Error Resume Next For i = 1 To ActiveDocument.InlineShapes.Count ' InlineShapes类型图片 ActiveDocument.InlineShapes(i).Height = 250 ' 设置图片高度为250px ActiveDocument.InlineShapes(i).Width = 420 ' 设置图片宽度420px Next i For i = 1 To ActiveDocument.Shapes.Count ' Shapes类型图片 ActiveDocument.Shapes(i).Height = Height ' 设置图片高度为 500px ActiveDocument.Shapes(i).Width = Weight ' 设置图片宽度 200px Next i End Sub ``` 此方法可以自定义高度和宽度的值,分别对嵌入式和浮动式图片进行设置 [^3]。 ### 方法四:根据条件设置图片大小 ```vba Sub SetPictureBorders() Dim n ' 图片个数 On Error Resume Next ' 忽略错误 For n = 1 To ActiveDocument.InlineShapes.Count ' InlineShapes 类型图片 If ActiveDocument.InlineShapes(n).Width > 394 Then ActiveDocument.InlineShapes(n).Range.Style = "Figure" ' 设置图片样式 ActiveDocument.InlineShapes(n).LockAspectRatio = msoTrue ' 锁定图片缩放比 ' ActiveDocument.InlineShapes(n).Width = 394 ' 设置图片宽度为394px ActiveDocument.InlineShapes(n).Borders.Enable = True ' 设置图片边框 End If Next n End Sub ``` 该方法会遍历嵌入式图片,当图片宽度大于394像素时,设置图片样式、锁定缩放比并启用边框 [^4]。 ### 方法五:统一设置图片大小 ```vba Sub AddNamespaceToMathMLInTextOnClipboard() Dim n Dim pixheight Dim pixwidth Dim pic As InlineShape For n = 1 To ActiveDocument.InlineShapes.Count pixheight = ActiveDocument.InlineShapes(1).Height pixwidth = ActiveDocument.InlineShapes(1).Width ActiveDocument.InlineShapes(n).Height = 80 ActiveDocument.InlineShapes(n).Width = 50 ActiveDocument.InlineShapes(n).LockAspectRatio = msoFalse Next n End Sub ``` 此方法会将所有嵌入式图片的高度设置为80像素,宽度设置为50像素,并且不锁定缩放比 [^5]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值