How to change colour of table header in PowerPoint by VBA excel

Chnage ppt table header VBA

Objective

To change colour of a table header in PowerPoint using VBA excel.

Approach

In this VBA Excel automation, we are changing the header cell colours of 3 PowerPoint tables. Here we have written the code in a .xlsm file, which will open the target ppt, and change the cells colours in all the tables on slide 1. We have created a command button on the excel macro file which has been assigned VBA code.

Steps

  1. Add the Microsoft library as shown in the image below.
  2. Select the source ppt by dialog box
  3. Loop through each shape in slide 1
  4. If shape is a table, then we shall change the colour of header cell until all header cell colour is changed
  5. Move to next shape in slide 1, then follow the same steps mentioned above.
  6. Save the file, then close the PowerPoint presentation

Code

Sub ChangeHeaderCellColour()

Application.ScreenUpdating = False
Dim obPptApp As PowerPoint.Application
Dim OpenPptDialogBox As Object
Dim ColumnCount As Integer



Set obPptApp = CreateObject("PowerPoint.Application")
Set OpenPptDialogBox = obPptApp.FileDialog(msoFileDialogOpen)

'Display message box to select a PPT
MsgBox ("Select a PPT from any location")


'Open the target PPT using dialog box
obPptApp.Activate
If OpenPptDialogBox.Show = -1 Then
obPptApp.Presentations.Open (OpenPptDialogBox.SelectedItems(1))
End If


'Change the header colour of table
For Each shp In obPptApp.ActivePresentation.Slides(1).Shapes
If shp.Type = msoTable Then

'Count the number of columns in the table
ColumnCount = obPptApp.ActivePresentation.Slides(1).Shapes(shp.Name).Table.Columns.Count

'Changing the colour for each cell in header
For i = 1 To ColumnCount
shp.Table.Cell(1, i).Shape.Fill.ForeColor.RGB = RGB(100, 150, 200)
Next i

End If
Next shp

obPptApp.ActivePresentation.Save

'Close the ppt
obPptApp.Windows(1).Close

End Sub

 

First we shall add library reference as shown in the image below.

 

Chnage ppt table header VBA

 

We have created a command button on the macro file. VBA code shown above is assigned to this button.

 

Chnage ppt table header VBA

 

Once we click on this command button, a message box will be displayed as shown in the image below.

 

Chnage ppt table header VBA

 

Once we click on “OK” in message box, a dialog box will appear as shown below.We shall select the target .ppt file by this.

 

Chnage ppt table header VBA

 

Before execution of the VBA code, all the tables have header of different colours.

 

Chnage ppt table header VBA

 

After the VBA code is executed, colour of header for all the 3 table changes into blue colour.We can see the same in the image below.

 

Chnage ppt table header VBA

 

Notes

  1. We can also change the colour of other cells in table as well .
  2. We can also define the path of ppt file using variables, but dialog box makes the code more dynamic

 

Reference

https://docs.microsoft.com/en-us/office/vba/api/excel.shape.fill

Post you may like

Copy file from one folder to another by copyfile method in VBA Excel