Word一键居中所有图片

新建一个宏,贴入如下代码,执行即可。如果文章中图片较多可能会卡顿。
Sub centerPictures()
Dim shpIn As InlineShape, shp As Shape
For Each shpIn In ActiveDocument.InlineShapes
shpIn.Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Next shpIn
For Each shp In ActiveDocument.Shapes
shp.Select
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
Next shp
End Sub

For something that turns out to be super-easy, there was not one place on the web that I could find this information. I needed to programmatically center all images in a Word document using Visual Basic for Applications (VBA). I searched high and low, and while I could find how to loop through all images, I couldn’t find how to center them. Then I turned to the macro writer’s best friend: the macro recorder. Duh! :blush:

It actually took me longer to find a document with pictures than it did to record the macro, see what it did, add it to my loops (see below), and test it.

You’ll notice that there are two loops in the code. That is so that it picks up inline figures as well wrapped figures.