VBA code for data in PowerPoint table, not objects

hobbes_

New Member
Joined
Jun 29, 2012
Messages
29
Hi guys,

I have this VBA code which I use for transferring data from my data sheet to slides in PowerPoint - it works fine.

However in PowerPoint I'm currently forced to transfer data to "text-objects" which is a bit of a drag because I'm used to working in a table in Powerpoint when organizing data.

Therefore my question is how to edit my VBA code so I can transfer data to a table in PowerPoint instead of an object.

Thanks. :)

Here's the VBA code:


Code:
Sub TDPTest()
    Dim shtStudent As Worksheet
    Dim strMedarbejder As String
    Dim strTitel As String
    Dim strFastholdelse As String
    Dim lngRow As Long
    Dim objPPT As Object
    Dim objPres As Object
    Dim objSld As Object
    Dim objShp As Object
    
    Set shtStudent = Worksheets("Ark1")
        
    Set objPPT = CreateObject("Powerpoint.Application")
    objPPT.Visible = True
    Set objPres = objPPT.presentations.Open(ThisWorkbook.Path & "\JBX_test.ppt")
    objPres.SaveAs ThisWorkbook.Path & "\TDPTest.ppt"
    
    lngRow = 2
    Do While shtStudent.Cells(lngRow, 2) <> ""
        
        strMedarbejder = shtStudent.Cells(lngRow, 1)
        strTitel = shtStudent.Cells(lngRow, 2)
        strFastholdelse = shtStudent.Cells(lngRow, 3)
                       
        Set objSld = objPres.slides(1).Duplicate
        For Each objShp In objSld.Shapes
            If objShp.HasTextFrame Then
                If objShp.TextFrame.HasText Then
                    objShp.TextFrame.TextRange.Replace "<Medarbejder>", strMedarbejder
                    objShp.TextFrame.TextRange.Replace "<Titel>", strTitel
                    objShp.TextFrame.TextRange.Replace "<Fastholdelse>", strFastholdelse
                End If
            End If
        Next
        lngRow = lngRow + 1
    Loop
    objPres.slides(1).Delete
    objPres.Save
    objPres.Close
    
End Sub
 
Last edited:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi

See if this example is useful. It needs some tweaking but the method is there...

Code:
Sub Excel_to_PP()
    Dim shtStudent As Worksheet, objPPT As Object, objPres As Object, nrows%, i%, j%, _
    mytable As PowerPoint.Shape, tabindex%
        
    Set shtStudent = Worksheets("Ark1")
    Set objPPT = CreateObject("Powerpoint.Application")
    objPPT.Visible = True
    Set objPres = objPPT.presentations.Open(ThisWorkbook.Path & "\JBX_test.pptm")
    objPres.SaveAs ThisWorkbook.Path & "\TDPTest.pptm"
    nrows = LastRow("Ark1") - 1                         ' will copy all data on sheet
    
    Set mytable = objPres.slides(1).Shapes.AddTable(nrows, 3)
    With objPres.slides(1).Shapes
        For i = 1 To .Count
            If .Item(i).HasTable Then
                tabindex = i                            ' find the table:
                Exit For                                ' I assume you have no other tables there...
            End If
        Next
    End With
        For i = 1 To nrows
            For j = 1 To 3
                objPres.slides(1).Shapes(tabindex).Table.Cell(i, j).Shape.TextFrame.TextRange.Text = _
                shtStudent.Cells(i + 1, j).Value
            Next
        Next
        objPres.Save
        objPres.Close
End Sub


Public Function LastRow(ByVal which$) As Long
    Sheets(which).Activate
    If WorksheetFunction.CountA(Cells) = 0 Then
        LastRow = 0
        Exit Function
    End If
    LastRow = Cells.Find(What:="*", After:=[a1], SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious).Row
End Function
 
Upvote 0
Hi Hobbes</SPAN></SPAN>

I downloaded the example files and will post a solution soon.</SPAN></SPAN>
My idea is to previously prepare a table in PowerPoint and populate copies of it with your data.</SPAN></SPAN>
 
Upvote 0
Hi Hobbes

I prepared a template table that you can see here:

https://www.dropbox.com/s/unxwy8puhqigjd2/JBX_test.ppt

Then this macro will do the job, please test it and report any problems...

Code:
Option Explicit


Sub Hobbes()
    Dim shtStudent As Worksheet, objPPT As Object, objPres As Object, nrows%, i%, j%, LastRow%
    
    Set shtStudent = Worksheets("Ark1")
    Set objPPT = CreateObject("Powerpoint.Application")
    objPPT.Visible = True
    Set objPres = objPPT.presentations.Open(ThisWorkbook.Path & "\JBX_test.ppt")
    objPres.SaveAs ThisWorkbook.Path & "\TDPFinal.ppt"
    LastRow = shtStudent.Range("b" & Rows.Count).End(xlUp).Row
    
    For i = 1 To LastRow - 2            ' first line of source data is a header
        objPres.Slides(1).Duplicate     ' create the necessary number of slides
    Next
        
    For i = 1 To LastRow - 1            ' each source data row
        For j = 1 To 3                  ' each source data column
            objPres.Slides(i).Shapes(1).Table.Cell(j + 1, 3).Shape.TextFrame.TextRange.Text = _
            shtStudent.Cells(i + 1, j).Value
        Next
    Next
    
    objPres.Save
    objPres.Close
    Set objPres = Nothing               ' clean up
    Set objPPT = Nothing
End Sub
 
Upvote 0
Hi Worf,

It works perfectly!! - thank you so much. :)

I'm going to end up with apx 20 columns of data. How do I add more columns to the macro and place the data in the right cells in the PowerPoint?

/H
 
Upvote 0
Hi Hobbes

I suggest you prepare a template table in PowerPoint, then run the following macro which will tell you the indexes of each cell. Knowing that you can modify my code to write on the desired cells.
If you feel it’s complicated, tell me and I’ll do it.

Code:
Sub TableCells()
Dim i%, j%, tbl As Table


' This is a PowerPoint macro, it fills a table´s cells with (#row, #column)
' assumes slide 2 has only one shape, the table, which can have merged cells


Set tbl = ActivePresentation.Slides(2).Shapes(1).Table


For i = 1 To tbl.Rows.Count
    For j = 1 To tbl.Columns.Count
        tbl.Cell(i, j).Shape.TextFrame.TextRange.Text = CStr(i) & ", " & CStr(j)
    Next
Next


End Sub
 
Upvote 0
I understand the principle, but executing is definitely above my competence level. Would be great if you could help. :)
 
Upvote 0
No problem, I can write it, but you must define for me the exact layout of the PowerPoint table and from what Excel cells the data is supposed to be imported.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top