Consulting

Results 1 to 2 of 2

Thread: Align a shape to the top right corner of the page

  1. #1
    VBAX Newbie
    Joined
    Jul 2021
    Posts
    2
    Location

    Align a shape to the top right corner of the page

    My vba macro for aligning a shape to the top right corner of the page in word isn't working


    Sub alignGraphicElementToTopRightCornerOfPage()
    '
    ' alignGraphicElementToTopRightCornerOfPage Macro
    '
    '
        Selection.ShapeRange.Align msoAlignTops, True
        Selection.ShapeRange.Align msoAlignRights, True
    End Sub
    See the difference between what happens when I use the macro, and what happens when I manually do the multiple commands from the ribbon.

    I created the macro using the record feature.

    The macro makes the item go off the page which is undesired behaviour, which is not what happened when I recorded the macro.

    word shape align macro.jpg

    Why isn't it working? I recorded the macro correctly. Is there any vba code I need to change or add to it?


    I'm using Word 2007

  2. #2
    VBAX Newbie
    Joined
    Jul 2021
    Posts
    2
    Location
    I've fixed the problem with an addition of adding new lines to the VBA macro.

    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
        ' Help formatting problems when the page has text on it,
        ' to prevent an extra line break 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 "wdWrapFront"
        
        Selection.ShapeRange.Align msoAlignTops, True
        Selection.ShapeRange.Align msoAlignRights, True
        ' start aligning the image to the top right of the page
        
    End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •