如何使用VBA从ppt中的某些形状导出文本?

时间:2013-05-03 08:41:44

标签: vba powerpoint powerpoint-vba

我正在尝试从大型ppt导出文本。我已经想出了如何导出,但我得到了所有形状的所有文本,我只对某些文本感兴趣。

有没有办法让IF函数检查形状的格式并仅在IF函数为真时才抓取文本。我想只从带有虚线边框的形状中选择文本。这可能吗?

这是我的代码

    Sub ExportText()

  Dim oPres As Presentation
  Dim oSlides As Slides
  Dim oSld As Slide         'Slide Object
  Dim oShp As Shape         'Shape Object
  Dim iFile As Integer      'File handle for output
  iFile = FreeFile          'Get a free file number
  Dim PathSep As String
  Dim FileNum As Integer

  #If Mac Then
    PathSep = ":"
  #Else
    PathSep = "\"
  #End If

  Set oPres = ActivePresentation
  Set oSlides = oPres.Slides

  FileNum = FreeFile

  'Open output file
  ' NOTE:  errors here if file hasn't been saved
  Open oPres.Path & PathSep & "AllText.TXT" For Output As FileNum

  For Each oSld In oSlides    'Loop thru each slide
    For Each oShp In oSld.Shapes                'Loop thru each shape on slide

      'Check to see if shape has a text frame and text
      If oShp.HasTextFrame And oShp.TextFrame.HasText Then
        If oShp.Type = msoPlaceholder Then
            Select Case oShp.PlaceholderFormat.Type
                Case Is = ppPlaceholderTitle, ppPlaceholderCenterTitle
                    Print #iFile, "Title:" & vbTab & oShp.TextFrame.TextRange
                Case Is = ppPlaceholderBody
                    Print #iFile, "Body:" & vbTab & oShp.TextFrame.TextRange
                Case Is = ppPlaceholderSubtitle
                    Print #iFile, "SubTitle:" & vbTab & oShp.TextFrame.TextRange
                Case Else
                    Print #iFile, "Other Placeholder:" & vbTab & oShp.TextFrame.TextRange
            End Select
        Else
            Print #iFile, vbTab & oShp.TextFrame.TextRange
        End If  ' msoPlaceholder
      End If    ' Has text frame/Has text

    Next oShp
  Next oSld

  'Close output file
  Close #iFile

End Sub

1 个答案:

答案 0 :(得分:2)

这里有适合你的解决方案。有关详细信息,请参阅代码中的注释。

Sub Partial_Solution()
'... your code here

'... your loops start here

    'this way check which DashStyle is in your interest,
    'there are lots of different Dash styles of line
    'then you could remove it
    Debug.Print oShp.Line.DashStyle

    'and this way you can check the style before reading from shape
    'put the result here, like 5 which is msoLineDashDot style
    If oShp.Line.DashStyle = 5 Then

        '... your code here

    End If

'... rest of your code here
End Sub