从VBA中的PowerPoint文件中提取所有文本
-
10-10-2019 - |
题
我有一组庞大的PowerPoint文件,我想从中提取所有文本,然后将其全部集中到一个大文本文件中。每个源(PPT)文件都有多个页面(幻灯片)。我不在乎格式,只有单词。
我可以用ppt中的 ^a ^c手动执行此操作,然后在记事本中进行 ^v。然后在PPT中分页,然后重复PowerPoint中的每个幻灯片。 (太糟糕了,我不能只做一个会抓住一切的 ^a ...然后我可以使用sendkey复制 /粘贴)
但是,有数百个具有不同幻灯片数量的PPT。
似乎这是一个普遍的事情,但是我在任何地方都找不到一个例子。
有人有示例代码吗?
解决方案
这是一些可以让您入门的代码。这将所有文本倒入幻灯片窗口。除了转储之外,它不会尝试格式化,分组或执行其他操作。
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
其他提示
下面的示例显示了基于上述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
不隶属于 StackOverflow