【问题标题】:Extract Text from Powerpoint to Excel将文本从 Powerpoint 提取到 Excel
【发布时间】:2021-04-04 11:10:59
【问题描述】:

我需要从 powerpoint 中提取一些文本到 excel 中,这是为了工作。我可以手动完成,但我相信有更好更快的方法。

我实际上并没有编码,我确实在 python 和 VBA 中做过一些类,但我并不真正精通它。我在网上找到了一些代码sigma code并尝试运行它,错误在于用户定义的类型未定义。

有人可以看看file 并指出正确的方向吗?如果我可以将每个文本框提取并发送到 excel 文件中的单独列中,那就太好了。


'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

【问题讨论】:

标签: excel vba powerpoint text-extraction


【解决方案1】:

您的用户定义错误可能是因为您没有使用 Tools->References 添加对 Excel 对象库的引用。此宏在 PPTM 文件中运行,不需要引用,因为它使用 late binding。它仅导出到新的工作簿文本框,每张幻灯片一行。

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

【讨论】:

  • 嗯,不知何故,我仍然在 Set xlApp = New Excel.Application 处收到用户定义的错误。我检查了 Excel 对象库,确实选择了对象库 16.0。它在你的一端运行吗?我只需要通过 pptm Visual Basic 运行它,一个新的 Excel 工作簿就会自动打开,对吗?我不确定是不是因为我的 Excel 没有真正正确打开,有一些加载项不再存在(一直在尝试禁用它们,但它并没有保持禁用状态)
  • @sara 你是说不能从“开始”图标手动打开 Excel。 ?
  • 不,我现在可以。对困惑感到抱歉。它手动打开。
  • @sara1 Set xlApp = New Excel.Application 不是我的代码,你会在哪一行单步执行我的代码。在 Windows 10 上使用 Office 2010 为我工作。
  • 嘿,代码运行良好。非常感谢。唯一的问题是它仅适用于我在上面的问题中附加的示例文件。它不适用于我的原始文件,我尝试玩弄并意识到当我选择原始幻灯片中的所有文本框,并使用“使用目标主题”的粘贴选项粘贴到新的演示文稿中时,它就可以工作了。但如果它是“保持源格式”,那么它就不起作用。对此有任何想法吗?我可以为每张幻灯片单独复制和粘贴文本框,但有更快的方法吗?
猜你喜欢
  • 2014-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多