狂自私

导航

自适应/调整图片大小为页面大小

Sub ResizeImagesToPageWidth()

    Application.ScreenUpdating = True
    ' 获取当前文档
    Set doc = ActiveDocument
    
    ' 获取页面宽度并减去页边距
    With doc.PageSetup
        pgWidth = .PageWidth - .LeftMargin - .RightMargin
    End With
    
    ' 遍历所有嵌入型图片
    For Each shp In doc.InlineShapes
        ' 只调整宽度大于页面宽度的图片
        If shp.Width > pgWidth Then
            ' 计算图片的宽高比
            aspectRatio = Abs(shp.Height / shp.Width)
            pgHeight = pgWidth * aspectRatio
            
            ' 调整图片宽度为页面宽度
            ' shp.LockAspectRatio = msoTrue
            If aspectRatio <> 0 Then
                shp.Width = pgWidth
                shp.Height = pgHeight
            
                ' 根据宽高比调整图片高度
                shp.Height = shp.Width * aspectRatio
            End If
        End If
    Next shp
    
    MsgBox "所有图片已调整为页面宽度!"
End Sub

 

posted on 2024-05-26 08:43  狂自私  阅读(2)  评论(0编辑  收藏  举报