Professionally Proper PowerPoint VBA

How to change the text size on all slides in
a presentation by using VBA.

 

If you are not familiar with the acronym VBA, it stands for Visual Basic for Applications and it is the programming language used by all Microsoft Office applications. With VBA you can perform many automation tasks that otherwise you would have to do manually, one item at a time.

 

 

Here is the proper VBA code for performing a font size change on all slides in a PowerPoint slide file:

Sample VBA Code for PowerPoint

Sample VBA Code for PowerPoint

One of the nice things about VBA is that it’s usually fairly readable; of course the person who wrote the code determines much of the readability factor. Let me review for you what this particular VBA subroutine is doing.

The first thing to note is that this is a public subroutine that I have called “ChangeFontSize”, if I had changed the designator from Public to Private then only another subroutine could execute it and you would not be able to run it from the Macros button on your PowerPoint Developer tab ribbon.

Next I have declared two object variables, one a PowerPoint Slide and the other a PowerPoint Shape; I have given each a shorthand reference name, sld and shp.


Now I have to provide you with a little background about objects, as they exist within PowerPoint. The slide object exists within a collection called “Slides” and the collection is all of the slides within the active PowerPoint file. An individual shape object on a single slide exists within a larger collection of all shapes on a slide. And in fact “ActivePresentation” is a single object within a potentially larger collection of all currently open PowerPoint files called “Presentations” but for this example I chose to work with only the currently active presentation.

To perform this change of text size on all slides in the PowerPoint file I have setup two nested loops. The first loop begins with “For Each sld…” and it’s meaning is that it will take each slide one at a time and examine it, when it is done, the enclosing “Next sld” command executes and the processing of the next slide begins.

So starting with slide 1 the code loops through all shapes on the slide and examines each to determine if has text and that the text is of the size we want to change.

In PowerPoint all items on the slide are Shapes and each shape has a distinct purpose. It might contain a Chart, a Picture, a Movie, Text, etc. and for this exercise we are only looking for Text thus the command “If shp.HasTextFrame”, which we then further examine to make sure there actually is text within the frame. Finding both of these conditions to be true, the existence of a text frame and that text exists within it, we then look at the range of text in the frame and determine if it is of the size we want to change. If we don’t do this check, then we would be changing ALL text regardless of its current size and we might not want to do that with slide titles that might, for example, be currently set to 36 point size.

Finally notice that conditional statements such as “If” are enclosed by an “End If” and the “For Each” loops are enclosed by “Next” commands. The indenting you see in this example is for readability; it is the “professionally proper” way to organize your code..