首页 文章

从VBA中的powerpoint文件中提取所有文本

提问于
浏览
5

我有一大堆powerpoint文件,我想从中提取所有文本,然后将它们全部集成到一个大文本文件中 . 每个源(PPT)文件都有多个页面(幻灯片) . 我不关心格式化 - 只关心单词 .

我可以通过PPT中的^ A ^ C手动执行此操作,然后在记事本中使用^ V;然后在PPT中向下翻页,并对powerpoint中的每张幻灯片重复 . (太糟糕了,我不能只做一个会 grab 一切的^ A然后我可以使用sendkey来复制/粘贴)

但是有数百个PPT具有不同数量的幻灯片 .

看起来这似乎是一件普通的事情,但我无法在任何地方找到一个例子 .

有没有人有示例代码来执行此操作?

2 回答

  • 3

    这里有一些代码可以帮助您入门 . 这会将幻灯片中的所有文本转储到调试窗口 . 除了转储之外,它不会尝试格式化,分组或执行任何操作 .

    Sub GetAllText()
    Dim p As Presentation: Set p = ActivePresentation
    Dim s As Slide
    Dim sh As Shape
    For Each s In p.Slides
        For Each sh In s.Shapes
            If sh.HasTextFrame Then
                If sh.TextFrame.HasText Then
                    Debug.Print sh.TextFrame.TextRange.Text
                End If
            End If
        Next
    Next
    End Sub
    
  • 1

    以下示例显示了基于上面给出的Otaku代码循环遍历文件列表的代码:

    Sub test_click2()
    
    Dim thePath As String
    Dim src As String
    Dim dst As String
    Dim PPT As PowerPoint.Application
    Dim p As PowerPoint.Presentation
    Dim s As Slide
    Dim sh As PowerPoint.Shape
    Dim i As Integer
    Dim f(10) As String
    
    f(1) = "abc.pptx"
    f(2) = "def.pptx"
    f(3) = "ghi.pptx"
    
    thePath = "C:\Work\Text parsing PPT\"
    
    For i = 1 To 3
      src = thePath & f(i)
      dst = thePath & f(i) & ".txt"
    
      On Error Resume Next
      Kill dst
      Open dst For Output As #1
        Set PPT = CreateObject("PowerPoint.Application")
        PPT.Activate
        PPT.Visible = True
        'PPT.WindowState = ppWindowMinimized
        PPT.Presentations.Open filename:=src, ReadOnly:=True
        For Each s In PPT.ActivePresentation.Slides
            For Each sh In s.Shapes
                If sh.HasTextFrame Then
                    If sh.TextFrame.HasText Then
                        Debug.Print sh.TextFrame.TextRange.Text
                    End If
                End If
            Next
        Next
        PPT.ActivePresentation.Close
      Close #1
    Next i
    Set PPT = Nothing
    
    End Sub
    

相关问题