运行的VBA程序如下:
Sub FormatSelectedPicture()
Dim shape As InlineShape
Dim floatShape As Shape
Dim picWidth As Single
' 获取选中的图片
Set shape = Selection.InlineShapes(1)
' 将图片设置为浮动并转换为Shape对象
Set floatShape = shape.ConvertToShape
' 设置布局为“浮于文字上方”
floatShape.WrapFormat.Type = wdWrapFront
' 将宽度调整为21厘米
picWidth = CentimetersToPoints(21)
floatShape.Width = picWidth
' 设置图片对齐页面、顶端对齐
floatShape.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
floatShape.Left = wdShapeCenter ' 水平居中对齐页面
floatShape.RelativeVerticalPosition = wdRelativeVerticalPositionPage
floatShape.Top = 0 ' 顶端对齐页面
End Sub