Powerpoint Macro to center align a single text box to slide - vba

I am trying to center align the text boxes in a large presentation. Each slide contains various shapes, but only has one text box with text in it, and I would like that text box aligned to the center of the slide. At the moment, I have a line of code that will make the text center aligned within its own text box, but I was wondering if there was a way of making the text box in the middle of the slide?
Sub TextSize()
Dim oSl As Slide
Dim oSh As Shape
With ActivePresentation
For Each oSl In .Slides
For Each oSh In oSl.Shapes
With oSh
If .HasTextFrame Then
If .TextFrame.HasText Then
.TextFrame.TextRange.Font.Size = 26.5
' change the code to make the text box centre aligned to the slide
.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignCenter
End If
End If
End With
Next
Next
End With
End Sub

Gopal's pointed you in the right direction. After your code to center the text, add this:
.Left = ActivePresentation.PageSetup.SlideWidth / 2 - .Width / 2
.Top = ActivePresentation.PageSetup.SlideHeight / 2 - .Height / 2

Although not mentioned, but in case you are adding the textboxes on the slide through VBA code (as explained in this documentation), then I guess textboxes do not have a property to directly get aligned to the slide in a particular fashion(in your case centrally).
Having said that, I would also point towards a possible workaround which may be to set the Top and the Left properties of the textboxes in a ratio proportional to the slide's height and width chosen for your presentation.
This I think should centrally align the textboxes.

Related

Text not resized when changing slide size

I would have a noob question about PowerPoint resizing.
I have a slide in "On screen Show (16:9)" size that I need to resize in "Widescreen" size, which is still 16:9 ratio but not the same dimensions.
So I changed the "Slide Size" option : the pictures are well resized, but the text font size in texboxes has not been changed proportionnaly.
My initial slide looks like this :
before resizing
But after resizing, the text font size in textboxes has not been changed :
after resizing
Would anyone have a solution to resize the same way all the element of a slide, including text ?
Thank you in advance.
I try this VBA code to have all textboxes with the option "Shrink text on overflow" :
Sub Change()
Dim oSlide As Slide
Dim oShape As Shape
For Each oSlide In ActivePresentation.Slides
For Each oShape In oSlide.Shapes
oshape.TextFrame2.AutoSize = MsoAutoSize.msoAutoSizeTextToFitShape
Next oShape
Next oSlide
End Sub
But that unfortunately doesn't work.

Referring subtitle of a slide in order to format, using VBA PowerPoint

I am trying to set the position and formatting of title, subtitle in the slide. I tried to change the position, font type of title and subtitle present in all slides using the following code. It works on title but there are no changes for subtitle position, font type. How can we get reference of subtitle to work on its position, font size etc.
Sub Titles()
Dim osld As Slide, oshp As Shape
For Each osld In ActivePresentation.Slides
For Each oshp In osld.Shapes
If oshp.Type = msoPlaceholder Then
If oshp.PlaceholderFormat.Type = ppPlaceholderTitle Then
With oshp
.Top = 5
.Left = 5
.TextFrame.TextRange.Font.Name = "Times New Roman"
.TextFrame.TextRange.Font.Size = 24
End With
ElseIf oshp.PlaceholderFormat.Type = ppPlaceholderSubtitle Then
With oshp
.Top = 10
.Left = 10
.TextFrame.TextRange.Font.Name = "Times New Roman"
.TextFrame.TextRange.Font.Size = 18
End With
End If
End If
Next oshp
Next osld
End Sub
The issue is almost certainly that most corporate templates use a placeholder with type ppPlaceholderBody rather than ppPlaceholderSubtitle for their subtitles. It’s not possible to add a ppPlaceholderSubtitle placeholder from the GUI, so template designers will usually just position an additional Body placeholder under the title.
The way we’ve dealt with identifying subtitles is:
Is the shape’s type a placeholder?
If yes, is it a Body placeholder?
If yes, is it within 50 pixels of both the expected Top and Left values for that placeholder, as it appears in the layout?
The third test is very important because users are always dragging, nudging, and repositioning template shapes. You have to include some level of wiggle when testing the position, otherwise you’ll miss lots of subtitles.

VBA for Microsoft PowerPoint to recognize and hide text boxes in German language

I need to make two presentations with the same slides, backgrounds, and everything except for the text: one in German and one in English. Therefore I have two separate presentations which I must always simultaneously update, otherwise one language version will be outdated and I often forget what I changed.
I have made a custom show with all of the slides copied into one PowerPoint presentation and that works fairly well, but I still must change two of the same slides each time that I make an edit to one language.
Therefore, I'm trying to write a macro that will recognize all textboxes within the presentation with German text in them and hide them during the show, and vice versa. Then, each of the macros would be linked to a hyperlinked box on the title slide called 'English' or 'German' which, when clicked, would hide the textboxes in the other languages while also leaving all pictures and formatting the same. Ideally, the macro would hide all boxes in one language and make all the boxes in the other language visible again within the same step, so that the presentation is always usable and I don't have a user who opens a PPT with 'no text boxes' because they would all be hidden...
In order to achieve this I have two text boxes containing the text in both languages on the same slide, that's why I'd like to hide the textboxes.
I am able to get all text boxes hidden but not text boxes in a specific language (aka, all boxes regardless of their editing language will get hidden but not any specific ones).
PS - text boxes do not NEED to be referenced here... it could just refer to a shape. I was trying to avoid, that Pictures would be hidden and thought text boxes would be the best way to reference what I wanted.
Is there a better way?
I don't really know how to reference a language in VBA, I found this website by accident and thought someone might have a quick trick to help me out with this issue. The code I have which will blend out textboxes but not blend out specific language-boxes is as follows:
Sub GermanTextBoxFinder()
Dim SlideToCheck As Slide
Dim ShapeIndex As Integer
' Visit each slide
For Each SlideToCheck In ActivePresentation.Slides
' On each slide, count down through the shapes
For ShapeIndex = SlideToCheck.Shapes.Count To 1 Step -1
' If the shape IS a text box and DOES have German text
If SlideToCheck.Shapes(ShapeIndex).Type = msoTextBox And _
MsoLanguageID.msoLanguageIDGerman _
Then
' Toggle visibility of German Textboxes
SlideToCheck.Shapes(ShapeIndex).Visible = msoFalse
End If
Next
Next
End Sub
Why don't you name the shapes with German text something that identifies them? E.g. use the prefix "txtGER" for the texts in German and "txtENG" for the ones in English. Then you could use something like the following:
If SlideToCheck.Shapes(ShapeIndex).Type = msoTextBox And _
Left(SlideToCheck.Shapes(ShapeIndex).Name, 6) = "txtGER" Then
' Toggle visibility of German Textboxes
SlideToCheck.Shapes(ShapeIndex).Visible = msoFalse
Else
SlideToCheck.Shapes(ShapeIndex).Visible = msoTrue
End If
(Please see this q+a for information on how to rename the shapes).
This will hide text boxes/shapes with text that contain either English or French. You can modify HideFrench to hide German instead ... Intellisense will provide the correct constants.
Sub HideEnglish()
Dim oSl As Slide
Dim oSh As Shape
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
If oSh.HasTextFrame Then
If oSh.TextFrame.HasText Then
If oSh.TextFrame.TextRange.LanguageID = msoLanguageIDEnglishUS Then
oSh.Visible = False
Else
oSh.Visible = True
End If
End If
End If
Next
Next
End Sub
Sub HideFrench()
Dim oSl As Slide
Dim oSh As Shape
For Each oSl In ActivePresentation.Slides
For Each oSh In oSl.Shapes
If oSh.HasTextFrame Then
If oSh.TextFrame.HasText Then
If oSh.TextFrame.TextRange.LanguageID = msoLanguageIDFrench Then
oSh.Visible = False
Else
oSh.Visible = True
End If
End If
End If
Next
Next
End Sub

Textbox with rounded corners using VBA

I have a predefined textbox in a PowerPoint slide, I need to make its corners rounded. Is there any way I can do this using VBA?
This example assumes that the shape you're after is the third shape on slide 1. Adjust accordingly:
Sub Test()
Dim oSh As Shape
Set oSh = ActivePresentation.Slides(1).Shapes(3)
With oSh
.AutoShapeType = msoShapeRoundedRectangle
.Adjustments(1) = 0.25
End With
End Sub
It converts the textbox to a rounded rectangle then sets the corners to be rounded. As far as I know, you cannot round the corners of a regular text box.

Centering a Shape using VBA in PowerPoint 2003

I am programatically copying tables and graphs from Excel to PowerPoint.
Ideally, I'd like to center the graphs on the slide and resize them to fit the slide if necessary.
This shouldn't be too hard, and I can check & modify .Top, .Left, .Width, .Height of the shape just fine, but how do I find out the width/height of the slide itself to do the proper positioning?
The following will give you the Height and Width ...
Just divide by 2 :D
Sub a()
Dim a As Presentation
Set a = ActivePresentation
MsgBox a.PageSetup.SlideHeight, a.PageSetup.SlideWidth
End Sub
HTH