1. 程式人生 > >VBA_批量調整圖片寬度

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