我的用於將形狀或圖形與 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

相關內容