【问题标题】:Unable to extract Powerpoint textbox contents into Excel using Excel VBA无法使用 Excel VBA 将 Powerpoint 文本框内容提取到 Excel 中
【发布时间】:2014-06-13 16:24:47
【问题描述】:

好的,到目前为止,我已经设法编写了以下代码,但是在以range 开头的行上,我不断收到424 object required 错误。谁能告诉我如何解决这个问题?

Sub GetText()     
    Set PPApp = GetObject(, "PowerPoint.Application")  
    i = 1
    Do While i <= PPApp.ActivePresentation.Slides(1).Shapes.Count
       If PPApp.ActivePresentation.Slides(1).Shapes(i).Type = msoTextBox Then
          range(Cells(i, 15)).Value = PApp.ActivePresentation.Slides(1).Shapes(i).TextFrame.TextRange.Text
       End If
       i = i + 1
    Loop 
End Sub

【问题讨论】:

  • Cells(i, 15).Value替换range(Cells(i, 15)).Value
  • 如果是 PPT 那么为什么要使用 range(Cells(i,15)),PPT 由 Shape、TextArea 组成。 Dim oRng As TextRange \n Set oRng = oShp.TextFrame.TextRange \n Set oTmpRng = oRng.Find( _ \n FindWhat:=searchtext, _ \n WholeWords:=False, _ \n matchcase:=matchvalue)
  • @simoco 谢谢你的建议,但我一直收到同样的错误
  • @Brain,感谢您的帮助...我对 vba 比较陌生,所以我不太确定如何将您的建议应用到我的代码中。如果您能帮助我,我将不胜感激!

标签: excel textbox powerpoint vba


【解决方案1】:

您的代码中有两个错误。

  1. Simoco 已经解决了您的第一个错误。即您需要使用Cells(i, 15).Value 而不是range(Cells(i, 15)).Value

  2. 你有一个错字。如果您使用了Option Explicit,那么您就会知道错误在哪里;)

您有 PPApp 作为 powerpoint 对象,但使用的是 PApp,因此使用的是 object required error

另外请声明您的变量并完全限定您的对象。

这是你正在尝试的吗?

Option Explicit

Sub GetText()
    Dim PPApp As Object
    Dim ws As Worksheet
    Dim i As Long

    Set PPApp = GetObject(, "PowerPoint.Application")

    i = 1

    '~~> Change this to the relevant sheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    Do While i <= PPApp.ActivePresentation.Slides(1).Shapes.Count
       If PPApp.ActivePresentation.Slides(1).Shapes(i).Type = msoTextBox Then
          ws.Cells(i, 15).Value = PPApp.ActivePresentation.Slides(1).Shapes(i).TextFrame.TextRange.Text
       End If
       i = i + 1
    Loop
End Sub

很少有其他观察结果。

  1. 您正在使用GetObject。如果有多个 powerpoint 实例,那么您很多都不会得到正确的结果。

  2. 即使形状不是 msoTextBox,您也在增加 i 的值。这可能会导致您在写入 Excel 工作表时跳过行。您可能希望使用不同的变量并在 If-EndiF

  3. 内递增该变量

【讨论】:

  • 非常感谢您,这太棒了!我也非常感谢额外的观察,并将确保我相应地调整宏
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-12
  • 2021-07-17
  • 1970-01-01
  • 2011-11-21
  • 2020-09-19
相关资源
最近更新 更多