Avatar of Suresh Agrawal
Suresh Agrawal
Flag for United States of America asked on

How to automate to insert the document title in each slide of the powerpoint presentation using VBA

I had this question after viewing Replacing text in PowerPoint 2016 using VBA.

Is it possible that I can insert a custom textbox in slide theme and it gets updated in actual slides ?
Here what I am trying to do in power point VBA which I have done successfully in Visio VBA.

I want to put the presentation file name on all slides of a presentation.  I have 50+ such presentations containing 25+ slides in each presentation.  The above code requires that I insert a text box on each of the slide manually and then it works if I run the macro.  Any suggestion how to automate the process using Powerpoint VBA ?

I do not want to change the slides theme for each presentation by inserting a text box with file name.  That would defeat the purpose of automation. Visio VBA does it very easily by using code in Auotexec module for each slidepack.
Microsoft VisioVBAMicrosoft PowerPoint

Avatar of undefined
Last Comment
Suresh Agrawal

8/22/2022 - Mon
Jamie Garroch (MVP)

You could simplify the process as follows:

1. Open the slide master "parent" by holding the Shift key and double-clicking the Normal view icon in the bottom right hand half of the status bar in PowerPoint.
2. Insert a text box into the parent slide of the master.
3. With the text box selected, insert the following macro (Alt+F11) and run it (Alt+F8)

' PowerPoint VBA Macro
' Author : Jamie Garroch of BrightCarbon (https://brightcarbon.com/)
' Date : 16JAN2019
' Purpose : insert the current presentation's file name into the selected shape
Sub InsertFileNameIntoSelectedShape()
  On Error GoTo errorhandler
  With ActiveWindow.Selection
    If .Type = ppSelectionShapes Or .Type = ppSelectionText Then
      With ActiveWindow.Selection.ShapeRange(1)
        If .HasTextFrame Then
          .TextFrame.TextRange.Text = ActivePresentation.Name ' or .FullName if path required
          GoTo quit
        End If
      End With
      GoTo quit
    End If
  End With
Exit Sub

  MsgBox "Select a shape that supprts text.", vbCritical + vbOKOnly, "Invalid Selection"
Exit Sub

  MsgBox "Error :" & Err & vbCrLf & vbCrLf & Err.Description, vbCritical + vbOKOnly, "Unhandled Error"

End Sub

Open in new window

Suresh Agrawal

Hi Jamie,

Your code works. I was just wondering if the textbox (shape) in the parent slide of the master can be given a fixed name and referred to that name in the code. So that we don't have to open the master for new presentation and run this manually. Anytime a new presentation is opened, the master will update the presentation name automatically in the text box.  This code then can possibly  be run in autoexec module in ppt VBA ?


Suresh Agrawal
Suresh Agrawal

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Suresh Agrawal

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.