Jay Hayers
Jay Hayers

Reputation: 47

Changing Specific Table Font via VBA in PowerPoint

Is there any reason this applies the font changes to all tables rather than the user inputted one? Thank you. No matter what I do it will always change every single table on every single slide to inputted font rather than every table with the same name on each slide. Thank you

Sub Button3()
  Dim bpFontName As String
    Dim bpSize
    Dim bpItem As String
    Dim oshp As Shape
Dim osld As Slide
Dim otbl As Table
Dim iRow As Integer
Dim iCol As Integer
Dim x As Long

    bpFontName = InputBox("Please enter font", "font type", "Calibri")
        bpSize = InputBox("Please enter font size", "fontsize", "12")
        bpItem = InputBox("Please enter the shape name", "shapename", "TextBox 1")

For Each osld In ActivePresentation.Slides
For Each oshp In osld.Shapes
With Shape
If Shape.Name = bpItem Then
If oshp.HasTable Then
Set otbl = oshp.Table
For iRow = 1 To otbl.Rows.Count
For iCol = 1 To otbl.Columns.Count
otbl.Cell(iRow, iCol).Shape.TextFrame.TextRange.Font.Size = bpSize
otbl.Cell(iRow, iCol).Shape.TextFrame.TextRange.Font.Name = bpFontName
Next iCol
Next iRow
End If
End If
End With
Next oshp
Next osld

Upvotes: 0

Views: 264

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25703

The problem is with these lines

With Shape
If Shape.Name = bpItem Then

Shape is not specific - it's a type of object, not something in the presentation. VBA is trying to guess what is wanted, as best it can.

The surrounding lines of code use oshp to refer to a specific Shape:

For Each oshp In osld.Shapes

and

If oshp.HasTable Then

Use oshp everywhere in the code in the For...Next loop to refer to each Shape object, in turn. In addition, when using With do not repeat the object, just use dot notation. Or do not use With:

For Each oshp In osld.Shapes
  With oshp
    If .Name = bpItem Then
      If .HasTable Then

Upvotes: 1

Related Questions