我的用于将形状或图形与 Word 中的页面右上角对齐的 vba 宏不起作用

我的用于将形状或图形与 Word 中的页面右上角对齐的 vba 宏不起作用
Sub alignGraphicElementToTopRightCornerOfPage()
'
' alignGraphicElementToTopRightCornerOfPage Macro
'
'
    Selection.ShapeRange.Align msoAlignTops, True
    Selection.ShapeRange.Align msoAlignRights, True
End Sub

查看使用宏时发生的情况与手动从功能区执行多个命令时发生的情况之间的区别。我使用记录功能创建了宏。

该宏使得项目超出页面范围,这是不受欢迎的行为,而这不是我录制宏时发生的情况。

microsoft word vba 宏将图形或形状对齐到页面的右上角

为什么不起作用?我正确录制了宏。我需要更改或添加任何 vba 代码吗?

答案1

我已经通过为 VBA 宏添加新行解决了该问题。

Sub alignGraphicElementToTopRightCornerOfPage()
'
' alignGraphicElementToTopRightCornerOfPage Macro
'
'
    Selection.ShapeRange.WrapFormat.Type = wdWrapFront
    ' choose the wrapping type
    ' the following values are good "3" "wdWrapFront" an "wdWrapTight"
    ' I don't know what "3" does but Microsoft Office 2007 uses it
    
    Selection.ShapeRange.RelativeHorizontalPosition = _
        wdRelativeHorizontalPositionColumn
    Selection.ShapeRange.RelativeVerticalPosition = _
        wdRelativeVerticalPositionPage
    Selection.ShapeRange.RelativeHorizontalSize = wdRelativeHorizontalSizePage
    Selection.ShapeRange.RelativeVerticalSize = wdRelativeVerticalSizePage
    ' these commands are needed if the wrapping type is "3" or "wdWrapFront"
    ' ensure the shape is aligned to the top right corner of the page
        ' This also fixes the formatting problems when the page has text on it,
        ' to prevent an extra line break or blank space appearing on the screen
        ' that the user did not intentionally create
    
    Selection.ShapeRange.WrapFormat.DistanceTop = CentimetersToPoints(0)
    Selection.ShapeRange.WrapFormat.DistanceBottom = CentimetersToPoints(0)
    ' these commands are needed if the wrapping type is "wdWrapTight"

    Selection.ShapeRange.Align msoAlignTops, True
    Selection.ShapeRange.Align msoAlignRights, True
    ' start aligning the image to the top right of the page
    
End Sub

相关内容