Hoan Cong
Hoan Cong

Reputation: 29

VBA center TextBox

I want to center text box in all slides like The picture please help me. I try this code but they're not work

Sub use2()
Set myDocument = ActivePresentation.SlideMaster
With myDocument.Shapes(1)
    .TextFrame.HorizontalAnchor = msoAnchorCenter
    .TextFrame.VerticalAnchor = msoAnchorMiddle
End With
End Sub

enter image description here

Upvotes: 0

Views: 324

Answers (2)

Bhavesh Shaha
Bhavesh Shaha

Reputation: 783

Have a for-loop incase you have multiples slides where the shape has to be centered. Make sure that the Name or Index of the shape is the same in all slides.

Edit: I misunderstood and assumed that OP wanted the text to be horizontally and vertically centered. They want the shape to be aligned to the center of the slide. I have edited the codes appropriately here:

Sub CenterTextBox()

For i = 1 to 10 'slidenumbers
On Error Resume Next
With ActivePresentation.Slides(i).Shapes("shapename")
   .Left = (ActivePresentation.PageSetup.SlideWidth - .Width) / 2
   .Top = (ActivePresentation.PageSetup.SlideHeight - .Height) / 2
End With
Next i

End Sub

Upvotes: 1

Najinsky
Najinsky

Reputation: 638

That will only work if the shape is on the master slide. If the shape is on a regular slide you need to reference that slide, by its index or id for example.

So

Set myDocument = ActivePresentation.Slides(1)

or

Set myDocument = ActivePresentation.Slides("name of slide")

Upvotes: 0

Related Questions