0

I need to extract some text from powerpoint into excel, it's for a work thing. I could do it manually but I am sure there is a much better and faster way.

I do not actually code, I did do some classes in python and VBA but I am not really proficient in it. I found some code online sigma code and tried to run it, there's an error in that the user-defined type being undefined.

Could someone take a look at the file and point me in the right direction? If I could just extract and send each text box into separate columns in the excel file, that would be great.


'Declare our Variables
Dim PPTPres As Presentation
Dim PPTSlide As Slide
Dim PPTShape As Shape
Dim PPTTable As Table
Dim PPTPlaceHolder As PlaceholderFormat

'Declare Excel Variables.
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlWrkSheet As Excel.Worksheet
Dim xlRange As Excel.Range

'Grab the Currrent Presentation.
Set PPTPres = Application.ActivePresentation
                     
    'Keep going if there is an error
    On Error Resume Next
    
    'Get the Active instance of Outlook if there is one
    Set xlApp = GetObject(, "Excel.Application")
    
        'If Outlook isn't open then create a new instance of Outlook
        If Err.Number = 429 Then
        
            'Clear Error
            Err.Clear
        
            'Create a new Excel App.
            Set xlApp = New Excel.Application
            
                'Make sure it's visible.
                xlApp.Visible = True
            
            'Add a new workbook.
            Set xlBook = xlApp.Workbooks.Add
            
            'Add a new worksheet.
            Set xlWrkSheet = xlBook.Worksheets.Add
    
        End If
    
    'Set the Workbook to the Active one, if Excel is already open. THIS ASSUMES WE HAVE A WORKBOOK IN THE EXCEL APP.
    Set xlBook = xlApp.Workbooks("ExportFromPowerPointToExcel.xlsm")
    
    'Set the Worksheet to the Active one, if Excel is already open. THIS ASSUMES WE HAVE A WORKSHEET IN THE WORKBOOK.
    Set xlWrkSheet = xlBook.Worksheets("Slide_Export")
    
    'Loop through each Slide in the Presentation.
    For Each PPTSlide In PPTPres.Slides
    
        'Loop through each Shape in Slide.
        For Each PPTShape In PPTSlide.Shapes
            
            'If the Shape is a Table.
            If PPTShape.Type = msoPlaceholder Or PPTShape.Type = ppPlaceholderVerticalObject Then
                
                'Grab the Last Row.
                Set xlRange = xlWrkSheet.Range("A100000").End(xlUp)

                'Handle the loops that come after the first, where we need to offset.
                If xlRange.Value <> "" Then

                    'Offset by One rows.
                    Set xlRange = xlRange.Offset(1, 0)

                End If

                'Grab different Shape Info and export it to Excel.
                xlRange.Value = PPTShape.TextFrame.TextRange
                xlRange.Offset(0, 1).Value = PPTSlide.Name
                xlRange.Offset(0, 2).Value = PPTSlide.SlideIndex
                xlRange.Offset(0, 3).Value = PPTSlide.Layout
                xlRange.Offset(0, 4).Value = PPTShape.Name
                xlRange.Offset(0, 5).Value = PPTShape.Type
                
            End If
            
        Next
    Next

    'Set the Worksheet Column Width.
    xlWrkSheet.Columns.ColumnWidth = 20
    
    'Set the Worksheet Row Height.
    xlWrkSheet.Rows.RowHeight = 20
    
    'Set the Horizontal Alignment so it's to the Left.
    xlWrkSheet.Cells.HorizontalAlignment = xlLeft
    
    'Turn off the Gridlines.
    xlApp.ActiveWindow.DisplayGridLines = False

End Sub

  • Does this answer your question? [Extracting text from PPT and pasting it in Excel using VBA](https://stackoverflow.com/questions/52570289/extracting-text-from-ppt-and-pasting-it-in-excel-using-vba) – Asif Kamran Malick Apr 04 '21 at 15:55

1 Answers1

0

Your user-defined error is probably because you haven't added a reference to the Excel Object Library using Tools->References. This macro runs in the PPTM file and doesn't need the reference as it uses late binding. It exports to new workbook text boxes only, one row for each slide.

Option Explicit

Sub ExportToExcel()

    'Declare variables
    Const WB_NAME = "ExportFromPowerPointToExcel.xlsx"
    Const WS_NAME = "Slide_Export"
   
    Dim PPTPres As Presentation, PPTSlide As Slide, PPTShape As Shape
    Dim PPTTable As Table
    Dim PPTPlaceHolder As PlaceholderFormat

    ' create workbook
    Dim xlApp, wb, ws
    Set xlApp = CreateObject("Excel.Application")
    Dim iRow As Long, c As Integer, folder As String
    
    'Set xlApp = New Excel.Application
    xlApp.Visible = True

    Set wb = xlApp.Workbooks.Add
    Set ws = wb.Worksheets(1)
    ws.Name = WS_NAME
    iRow = 2

    'Grab the Currrent Presentation.
    Set PPTPres = Application.ActivePresentation

    'Loop through each Slide in the Presentation.
    For Each PPTSlide In PPTPres.Slides
    
        'Loop through each Shape in Slide.
        For Each PPTShape In PPTSlide.Shapes
            If PPTShape.HasTextFrame Then
                c = PPTShape.Id + 1
                ' headings
                If ws.Cells(1, c) = "" Then
                   ws.Cells(1, c) = PPTShape.Name
                End If
                ws.Cells(iRow, c) = PPTShape.TextFrame.TextRange
            End If
        Next
        ws.Cells(iRow, 1) = PPTSlide.Name
        iRow = iRow + 1

    Next

    With ws
        .Columns.ColumnWidth = 20
        .Rows.RowHeight = 20
        .Columns.HorizontalAlignment = xlLeft
    End With
    xlApp.ActiveWindow.DisplayGridLines = False

    ' save
    folder = PPTPres.Path & "\"
    xlApp.DisplayAlerts = False
    wb.SaveAs folder & WB_NAME
    xlApp.DisplayAlerts = True
    wb.Close False

    ' quit excel
    xlApp.Quit
    Set xlApp = Nothing

    MsgBox "File saved to " & folder & WB_NAME
End Sub
CDP1802
  • 13,871
  • 2
  • 7
  • 17
  • Hmm somehow I am still getting that user-defined error at Set xlApp = New Excel.Application. I checked the Excel Object Library and have indeed selected the Object library 16.0. Does it run on your end? I just have to run this through the pptm visual basic and a new excel workbook will automatically open right? I'm not sure if it's because my Excel doesn't really open up properly, there're some add-in's that are no longer there (been trying to disable them but it doesn't stay disabled) – sara1934-hasbrown Apr 05 '21 at 14:27
  • @sara Are you saying you can't open Excel manually from the Start icon. ? – CDP1802 Apr 05 '21 at 14:50
  • No, I can now. Sorry for the confusion. It opens manually. – sara1934-hasbrown Apr 05 '21 at 14:53
  • @sara1 `Set xlApp = New Excel.Application` is not my code, what error and on which line do you get single stepping my code. Works for me with Office 2010 on windows 10. – CDP1802 Apr 05 '21 at 16:35
  • Hey, the code works perfectly fine. Thank you so much. Only problem is that it only works on the sample file which I attached above in my question. It doesn't work on my original file, I tried playing around and realised that when I select all the text boxes in the original slide, and paste into a new presentation using paste option of "using destination theme" then it works. But if it is "keeps source formatting" then it doesn't work. Any idea on this? I could copy and paste the text boxes individually for each slide but is there any faster way? – sara1934-hasbrown Apr 11 '21 at 06:44
  • I ran the first code above, the one by Sigma Code, and it manages to pick out everything, except that it's not one row for each slide, it's all placed in the same column separate row. For example, each slide has 10 placeholders and all 10 are falling in the same column, one row after another – sara1934-hasbrown Apr 11 '21 at 08:06
  • @sara try using msoPlaceholder instead of msoTextBox as the shape type in my code – CDP1802 Apr 11 '21 at 08:34
  • Hey it almost works. It works for the first 5 slides and then stops. There's a run-time error. The specified value is out of range. Error is with this line: ws.Cells(iRow, c) = PPTShape.TextFrame.TextRange – sara1934-hasbrown Apr 11 '21 at 09:10
  • @sara see my updated answer I have used `If PPTShape.HasTextFrame` rather than `PPTShape.Type = msoPlaceholder`. Placeholders don't always have text. – CDP1802 Apr 11 '21 at 10:38
  • It works now! Thank you so much!! You just saved me so much time! – sara1934-hasbrown Apr 11 '21 at 12:36