2011-01-12 43 views
4

我有一套庞大的PowerPoint文件,我想从中提取所有文本,并将其全部整合到一个大文本文件中。每个源(PPT)文件都有多个页面(幻灯片)。我不在乎格式化 - 只有单词。从VBA中的Powerpoint文件中提取所有文本

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

但是有很多数百个PPT具有不同数量的幻灯片。

看起来这将是一件常见的事情想做,但我无法在任何地方找到一个例子。

有没有人有示例代码来做到这一点?

回答

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 
+0

我给它一个尝试,回来! – elbillaf 2011-01-13 03:30:28

+0

很酷。请注意,调试窗口中可以容纳的文本数量有限。你可以将结果踢出.txt或其他文件。 – 2011-01-13 05:44:17

1

下面的例子显示了通过基于宅男的代码文件的列表代码回路上面给出:

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