vbapowerpoint

How to make a Table of 200 rows in PowerPoint Application


I want to make a Table of 200 rows in PowerPoint Application.

The following code throws this error: Run-time error 424: Object requried

How to solve that error?


Solution

  • I copied your code and did a test and had the same issue, interestingly several times at index 170 (but that could be by coincidence).

    When the code stopped with that error and I pressed "Debug", I always could continue (by using F5) and the code executed successfully.

    Somehow Powerpoint was not fast enough to handle the VBA requests. I simply added a DoEvents inside the loop and the issue was gone.

    I modified your code a little bit. It makes your life as programmer much easier when you work with object variables. This is what I have:

    ' Add a slide
    Dim slide As Slide
    Set slide = ActivePresentation.Slides.Add(Index:=1, Layout:=ppLayoutBlank)
    ' Add a Table
    Dim tblshape As Shape, table As Table
    Set tblshape = slide.Shapes.AddTable(NumRows:=1, NumColumns:=1, Left:=5, Top:=5, Width:=900, Height:=500)
    shape.Name = "tbl"
    Set table = tblshape.table
    
    ' Make 200 rows
    Do While table.Rows.Count < 200
        table.Rows.Add
    Loop
    ' Resize and fill table rows
    For i = 1 To table.Rows.Count
        DoEvents  ' Give Powerpoint a chance.
        Dim row As Row
        Set row = table.Rows(i)
        row.Height = 8
        row.Cells(1).shape.TextFrame.TextRange.Text = i
    Next i