PowerPoint VBA在Notes中搜索和删除段落

时间:2015-11-19 04:24:28

标签: powerpoint-vba

我有几个PowerPoints,笔记中有大量文字。我需要搜索注释文本并删除任何以“A”开头的段落。

这是我尝试的 - 但是我遇到了类型不匹配错误

  Dim curSlide As Slide
  Dim curNotes As Shape
  Dim x As Long

  For Each curSlide In ActivePresentation.Slides
    Set curNotes = curSlide.NotesPage.Shapes(2).TextFrame.TextRange

    With curNotes.TextFrame.TextRange
        For x = 1 To Len(curNotes.TextFrame.TextRange)
            If Mid(curNotes.TextFrame.TextRange, x, 2) = "A." Then
                curNotes.TextFrame.TextRange.Paragraphs = ""
            End If
        Next x
    End With

  Next curSlide

End Sub

感谢您的帮助!!

1 个答案:

答案 0 :(得分:0)

每当您尝试分配变量指定的其他类型的数据时,都会出现不匹配错误。这是在您的代码中发生的,因为您将curNotes定义为Shape类型,然后尝试将该对象变量设置为不同的数据类型TextRange。然后,您尝试将对象TextRange作为字符串处理。你需要处理.TextRange的.Text子项使用Mid不检查字符串的开头,最后,当你将文本设置为“”时,你删除了Note中的所有文本,但那不是你的意思说你想做。

这是更正的代码,只删除以“A。”开头的段落。

' PowerPoint VBA macro to delete all slide note paragraphs starting with the string "A."
' Rewritten by Jamie Garroch of youpresent.co.uk
Option Explicit

Sub DeleteNoteParagraphsStartingA()
  Dim curSlide As Slide
  Dim curNotes As TextRange
  Dim iPara As Long

  For Each curSlide In ActivePresentation.Slides
    Set curNotes = curSlide.NotesPage.Shapes(2).TextFrame.TextRange

    With curNotes
      ' Count backwards in any collection when deleting items from it
      For iPara = .Paragraphs.Count To 1 Step -1
        If Left(.Paragraphs(iPara), 2) = "A." Then
          .Paragraphs(iPara).Delete
          Debug.Print "Paragraph " & iPara & " deleted from notes pane on slide " & curSlide.SlideIndex
        End If
      Next
    End With

  Next curSlide
End Sub