Search code examples
vbapowerpoint

How to copy and store the position of an object / shape in PPT and then paste that position onto other objects in other slides


I'm new to VBA coding, and I want to create two macros, one that stores an objects / shapes position, and another that allows me to paste that positioning onto other objects on any slide (including other slides). I think the issue I am running into is referencing the stored objects position from the other code, but I am very new to this so i could have just coded gibberish haha. Thanks!

Sub CopyPositon()
    
    Dim oshpR  As ShapeRange
    Dim T1     As Single
    Dim L1     As Single
    
    Set oshpR = ActiveWindow.Selection.ShapeRange
    
    T1 = oshpR(1).Top
    
    L1 = oshpR(1).Left

End Sub

Sub PastePosition()

    Set oshpR = ActiveWindow.Selection.ShapeRange

    oshpR.Left = L1

    oshpR.Top = T1

End Sub

Solution

    • In VBE Insert>Module, paste the code.
    • Run CopyPositon first, then run PastePosition
    Public T1     As Single
    Public L1     As Single
    Sub CopyPositon()
        Dim oshpR  As ShapeRange
        Set oshpR = ActiveWindow.Selection.ShapeRange
        T1 = oshpR(1).Top
        L1 = oshpR(1).Left
    End Sub
    Sub PastePosition()
        Set oshpR = ActiveWindow.Selection.ShapeRange
        oshpR.Left = L1
        oshpR.Top = T1
    End Sub
    

    If you could keep a placeholder (assumes it is the first shape) on slide one as reference, you don't have to use public variable.

    Sub SetPosition()
        Dim oshpR  As ShapeRange
        Dim T1     As Single
        Dim L1     As Single
        With ActivePresentation.Slides(1).Shapes(1)
                T1 = .Top
                L1 = .Left
        End With
        Set oshpR = ActiveWindow.Selection.ShapeRange
        oshpR.Left = L1
        oshpR.Top = T1
    End Sub