Solved Working with a PowerPoint table (1 Viewer)

The Rev

Registered User.
Local time
Today, 18:28
Joined
Jan 15, 2003
Messages
118
Good morning,

I have a PowerPoint slide that has a table as one of the elements on the slide. I need to select the table and then replace the string "<Co Name>" with my stored variable of CompanyName. I don't know how to call the table and loop through the table cells for that string. Here's what I have thus far that doesn't seem to be working...

Code:
Set CurSlide = objPresentation.Slides(13).Select()
Dim t As Table
Dim tRow As Long, iCol As Long
Dim tText As TextRange
Dim tTemp As TextRange
With t
For tRow = 1 To .Rows.Count
For tCol = 1 To .Columns.Count
Set tText = .Cell(tRow, tCol).Shape.TextFrame.TextRange
Do
Set tTemp = tText.Replace("<Co Name>", CompanyName, , WholeWords, MatchCase)
Loop While Not tTemp Is Nothing
Next tCol
Next tRow
End With
 

The Rev

Registered User.
Local time
Today, 18:28
Joined
Jan 15, 2003
Messages
118
Still no idea why this isn't working...
 

Minty

AWF VIP
Local time
Today, 23:28
Joined
Jul 26, 2013
Messages
10,371
You aren't actually setting your table variable to anything... And before that you would need to then set the table as a shape object.

Rich (BB code):
Dim oSh as Shape

Set oSh = CurSlide.Shapes("YourTableName")
Set t = oSh.Table
With t ....
 

The Rev

Registered User.
Local time
Today, 18:28
Joined
Jan 15, 2003
Messages
118
Still no luck. Here's the table by itself on a slide.
 

Attachments

  • Schedule.zip
    32.6 KB · Views: 364

Minty

AWF VIP
Local time
Today, 23:28
Joined
Jul 26, 2013
Messages
10,371
Why not use the code I provided before with a tiny alteration?

SQL:
Sub ChangeText()
        
    Dim t As Table
    Dim tRow As Long, tCol As Long
    Dim tText As TextRange
    Dim tTemp As TextRange
    Dim sTemplate As String
    Dim objPPT As Object
    Dim oSh As Object
    Dim CompanyName As String
    Dim FindWord As String
    Dim iWords As Long
    
    
    FindWord = "<Co Name>"
    
    sTemplate = "C:\AccessForums\Schedule.pptx" ' Change to suit your file
    
    CompanyName = "M$oft Corp"
    
    Set objPPT = CreateObject("PowerPoint.Application")
      
    Set ppPres = objPPT.Presentations.Open(sTemplate)
    
    With ppPres.Slides(1)
        Set oSh = .Shapes("Table5")
        Set t = oSh.Table
    
        With t
            For tRow = 1 To .Rows.Count
                For tCol = 1 To .Columns.Count
                    Debug.Print tRow, tCol
                    Set tText = .Cell(tRow, tCol).Shape.TextFrame.TextRange.Find(FindWord)
                    
                    If Not (tText Is Nothing) Then 'if something is found
                        tText.Text = CompanyName      'then replace it
                        iWords = iWords + 1
                    End If
                    
               Next tCol
            Next tRow
        End With
    End With
    
End Sub
 

The Rev

Registered User.
Local time
Today, 18:28
Joined
Jan 15, 2003
Messages
118
I tinkered and finally got it to work.

Code:
Dim t As Table
Dim tRow As Long
Dim iCol As Long
Dim oSh As Shape
Dim tText As String

For Each objShape In objPresentation.Slides(13).Shapes
    Set t = oSh.Table

    With t
        For tRow = 1 To objShape.Table.Rows.Count
            For tCol = 1 To objShape.Table.Columns.Count
                objShape.Table.Cell(tRow, tCol).Shape.TextFrame.TextRange = Replace(objShape.Table.Cell(tRow, tCol).Shape.TextFrame.TextRange, "<Co Name>", CompanyName)
            Next tCol
        Next tRow
    End With
Next objShape
 

Users who are viewing this thread

Top Bottom