Need help with using vba macro to align graphic to top right of page


  1. Posts : 255
    Windows 10
       #1

    Need help with using vba macro to align graphic to top right of page


    Code:
    Sub alignGraphicElementToTopRightCornerOfPage()
    '
    ' alignGraphicElementToTopRightCornerOfPage Macro
    '
    '
        Selection.ShapeRange.Align msoAlignTops, True
        Selection.ShapeRange.Align msoAlignRights, True
    End Sub
    I need help with using a vba macro to align a graphic (or shape) to the top right of the page.

    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.

    Need help with using vba macro to align graphic to top right of page-word-shape-align-macro.gif

    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 Microsoft Office 2007
      My Computer


  2. Posts : 1,223
    W10-Pro 22H2
       #2

    Tested using Word 2010: those 2 lines work for me. All I can say is that the graphic must be set to wrap text in some way - if it is set to 'inline with text' (as is my usual choice), it won't shift.
      My Computer


  3. Posts : 18,044
    Win 10 Pro 64-bit v1909 - Build 18363 Custom ISO Install
       #3

    Hello @desbest,

    It worked OK for me on Word 2007.
      My Computer


  4. Posts : 255
    Windows 10
    Thread Starter
       #4

    I've fixed the problem with an addition of adding new lines to the VBA macro.

    Code:
    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
    Last edited by desbest; 26 Jul 2021 at 07:03.
      My Computer


  5. Posts : 1,223
    W10-Pro 22H2
       #5

    For the benefit of others: this is the line that turns an in-line object into a floater, the only addition I found necessary:
    Code:
    Selection.ShapeRange.WrapFormat.Type = wdWrapFront
    Thanks for the question, I have learnt something! Martin
      My Computer


  6. Posts : 255
    Windows 10
    Thread Starter
       #6

    mngerhold said:
    For the benefit of others: this is the line that turns an in-line object into a floater, the only addition I found necessary:
    Code:
    Selection.ShapeRange.WrapFormat.Type = wdWrapFront
    Thanks for the question, I have learnt something! Martin
    Just because only adding one line works for you, doesn't mean that you should recommend that other people should do the same. Your one extra line won't always work for everyone to be applicable for every scenario. My updated macro that you see in the previous post has lots of additional lines for a reason. There's a myriad of ways that a shape or graphic can be formatted, beyond just the text wrapping setting.

    Need help with using vba macro to align graphic to top right of page-word-text-wrapping-types.png

    Watch the video I made below to illustrate this, why the extra lines I added are necessary. When adding just the one line you suggested, the expected behaviour of the macro fails and the graphic fails to move to the top right corner of the screen.

    Anyway I have updated my macro code to make the comments more explanatory as to what the commands do and why they're there.

    Last edited by desbest; 26 Jul 2021 at 11:57.
      My Computer


  7. Posts : 16,949
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #7

    desbest said:
    … doesn't mean that you should recommend that other people should do the same. ...
    He didn't.

    Denis
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 20:26.
Find Us




Windows 10 Forums