思涯谷

  • 首页
  • 探索
  • 标签
  • 关于
思涯谷 ©2025
京ICP备2022030312号GitHub User's stars

Word宏保持纵横比更改选中图片宽度

在保持图片纵横比的情况下,将选定的图片宽度调整为4.7厘米。

...
标签:Word
点赞(0)
返回顶部
2024-11-11

留言

Sub ResizePictureKeepAspectRatio()
    Dim shp As InlineShape
    Dim newWidth As Single
    Dim currentHeight As Single
    Dim currentWidth As Single
    Dim aspectRatio As Single
    
    ' 设置新的宽度(单位:厘米)
    newWidth = CentimetersToPoints(4.7)
    
    ' 检查是否有选中的图片
    If Selection.InlineShapes.Count > 0 Then
        Set shp = Selection.InlineShapes(1)
        
        ' 获取当前的高度和宽度(单位:磅)
        currentWidth = shp.Width
        currentHeight = shp.Height
        
        ' 计算纵横比
        aspectRatio = currentHeight / currentWidth
        
        ' 调整图片宽度并保持纵横比
        With shp
            .Width = newWidth
            .Height = newWidth * aspectRatio
        End With
    End If
End Sub