Deeef
Deeef

Reputation: 11

Get the precise x/y position of guides for precisely aligning shapes to them using VBA

Is there any way to get the precise - as in including decimal places - x/y position of a guideline using VBA?

When getting the position of a guides in PPT via VBA, the result is not as precise as required. Decimal places are omitted. This is confusing me, since the variable type is "Single" which should incorporate decimal places.

The overarching goal is to "snap" shapes of all kinds to guides using VBA. Use case: User selects one or multiple shapes and wants to align them all to the leftmost guide in the presentation by just clicking a button.

My code works fine thus far, the only issue being that the shapes don't get precisely aligned with the guides.

Sub align_left()
    Dim shp As Shape
    Dim gd As Guide
    With ActiveWindow.Selection
        On Error Resume Next
        x_gd = ActiveWindow.Width

        MsgBox x_gd

        For Each gd In ActivePresentation.SlideMaster.Guides
            If gd.Orientation = ppVerticalGuide And gd.Position < x_gd Then
                x_gd = gd.Position
            End If
        Next

        MsgBox x_gd

        For Each shp In .ShapeRange
            shp.Left = x_gd
        Next

    End With
End Sub

Expected: Shapes "snap" to guides. Actual: Shapes are moved CLOSE to the guides but depending on guide position, there's still a gap between guides and shapes.

Upvotes: 1

Views: 729

Answers (1)

Oran G. Utan
Oran G. Utan

Reputation: 466

I would say to round to 4 decimals, as dimensions are set as Single values

            shp.Left = Round(x_gd, 4)
            shp.Top = Round(y_gd, 4)

Upvotes: 0

Related Questions