我有一个 docx 文件,里面有很多图片,但都不适合文档的边距。我可以手动调整文件中图片的大小,但希望能有某种方式来自动执行此操作(无论是通过 Word、命令行工具还是其他任何方式)。
(PS:这是这个问题)
答案1
阅读Word 中的 Visual Basic 宏用于调整大小/居中/删除所有图像,如何调整Word文档中所有图像的大小和如何调整表格大小以适合页面宽度稍微修复了 Kelly Tessena Keck 解决方案。
现在它可以适用于任何可用的页面宽度(如果需要,也不要忘记修复高度):
Sub PicturesFitPageWidth()
' ResizePic Macro
' Resizes an image
Shapes = ActiveDocument.Shapes.Count
InLines = ActiveDocument.InlineShapes.Count
'Sets the variables to loop through all shapes in the document, one for shapes and one for inline shapes.
'Calculate usable width of page
With ActiveDocument.PageSetup
WidthAvail = .PageWidth - .LeftMargin - .RightMargin
End With
For ShapeLoop = 1 To Shapes
MsgBox Prompt:="Shape " & ShapeLoop & " width: " & ActiveDocument.Shapes(ShapeLoop).Width
If ActiveDocument.Shapes(ShapeLoop).Width > WidthAvail Then
ActiveDocument.Shapes(ShapeLoop).Width = WidthAvail
End If
Next ShapeLoop
'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.
For InLineLoop = 1 To InLines
MsgBox Prompt:="Inline " & InLineLoop & " width: " & ActiveDocument.InlineShapes(InLineLoop).Width
If ActiveDocument.InlineShapes(InLineLoop).Width > WidthAvail Then
ActiveDocument.InlineShapes(InLineLoop).Width = WidthAvail
End If
Next InLineLoop
'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.
End Sub
答案2
您可以使用以下 VBA 代码执行此操作。它会计算文档中的形状数量,根据页面上的可用空间检查它们的宽度,并在必要时调整大小。
请注意,Word 有两个不同的集合Shapes
和InlineShapes
,因此有两个不同的For
循环。此外,它使用一系列语句根据标准纸张尺寸识别页面宽度。目前,唯一的选项是纵向或横向的信纸尺寸,但您可以根据需要If/ElseIf
添加更多纸张尺寸。ElseIfs
Sub ResizePic()
' ResizePic Macro
' Resizes an image
Shapes = ActiveDocument.Shapes.Count
InLines = ActiveDocument.InlineShapes.Count
'Sets the variables to loop through all shapes in the document, one for shapes and one for inline shapes.
RightMar = ActiveDocument.PageSetup.RightMargin
LeftMar = ActiveDocument.PageSetup.LeftMargin
PaperType = ActiveDocument.PageSetup.PaperSize
PageLayout = ActiveDocument.PageSetup.Orientation
'Sets up variables for margin sizes, paper type, and page layout.
' This is used to find the usable width of the document, which is the max width for the picture.
If PaperType = wdPaperLetter And PageLayout = wdPortrait Then
WidthAvail = InchesToPoints(8.5) - (LeftMar + RightMar)
ElseIf PaperType = wdPaperLetter And PageLayout = wdLandscape Then
WidthAvail = InchesToPoints(11) - (LeftMar + RightMar)
End If
'Identifies the usable width of the document, based on margins and paper size.
For ShapeLoop = 1 To Shapes
MsgBox Prompt:="Shape " & ShapeLoop & " width: " & ActiveDocument.Shapes(ShapeLoop).Width
If ActiveDocument.Shapes(ShapeLoop).Width > WidthAvail Then
ActiveDocument.Shapes(ShapeLoop).Width = WidthAvail
End If
Next ShapeLoop
'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.
For InLineLoop = 1 To InLines
MsgBox Prompt:="Inline " & InLineLoop & " width: " & ActiveDocument.InlineShapes(InLineLoop).Width
If ActiveDocument.InlineShapes(InLineLoop).Width > WidthAvail Then
ActiveDocument.InlineShapes(InLineLoop).Width = WidthAvail
End If
Next InLineLoop
'Loops through all shapes in the document. Checks to see if they're too wide, and if they are, resizes them.
End Sub