To change colour of a table header in PowerPoint using VBA excel.
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.
- Add the Microsoft library as shown in the image below.
- Select the source ppt by dialog box
- Loop through each shape in slide 1
- If shape is a table, then we shall change the colour of header cell until all header cell colour is changed
- Move to next shape in slide 1, then follow the same steps mentioned above.
- Save the file, then close the PowerPoint presentation
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.
We have created a command button on the macro file. VBA code shown above is assigned to this button.
Once we click on this command button, a message box will be displayed as shown in the image below.
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.
Before execution of the VBA code, all the tables have header of different colours.
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.
- We can also change the colour of other cells in table as well .
- We can also define the path of ppt file using variables, but dialog box makes the code more dynamic
Post you may like