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.

CC BY-NC-SA 4.0 Word一键居中所有图片 by 桔子小窝 is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

发表评论

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据