Mein VBA-Makro zum Ausrichten einer Form oder Grafik an der oberen rechten Ecke der Seite in Word funktioniert nicht

Mein VBA-Makro zum Ausrichten einer Form oder Grafik an der oberen rechten Ecke der Seite in Word funktioniert nicht
Sub alignGraphicElementToTopRightCornerOfPage()
'
' alignGraphicElementToTopRightCornerOfPage Macro
'
'
    Selection.ShapeRange.Align msoAlignTops, True
    Selection.ShapeRange.Align msoAlignRights, True
End Sub

Sehen Sie den Unterschied zwischen dem, was passiert, wenn ich das Makro verwende, und dem, was passiert, wenn ich die verschiedenen Befehle manuell über das Menüband ausführe. Ich habe das Makro mithilfe der Aufzeichnungsfunktion erstellt.

Das Makro führt dazu, dass das Element von der Seite verschwindet. Dies ist ein unerwünschtes Verhalten, das jedoch nicht passiert ist, als ich das Makro aufgezeichnet habe.

Microsoft Word VBA-Makro: Grafik oder Form an der oberen rechten Ecke der Seite ausrichten

Warum funktioniert es nicht? Ich habe das Makro korrekt aufgezeichnet. Muss ich VBA-Code ändern oder hinzufügen?

Antwort1

Ich habe das Problem durch das Hinzufügen neuer Zeilen für das VBA-Makro behoben.

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

verwandte Informationen