Powerpoint 循环折线图 - Active X 错误

Powerpoint loop through line charts - Active X Error

我编写了一个代码,用于从 excel 打开 Powerpoint,然后遍历所有幻灯片,找到图表并更改一些列。我有进行替换的代码,但似乎无法循环播放幻灯片,因为它抛出 ActiveX 错误 429,基本上是说找不到 powerpoint :O.

Sub pptDataChange()    

'Define variables of excel
Dim mySheet As Excel.Worksheet

'Define variables to open on PPT
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Dim sld As Slide
Dim shp As Shape
Dim chrt As Chart

'Copy range from Excel
Set mySheet = ThisWorkbook.Worksheets("Sheet1")

'Create instance of Powerpoint
On Error Resume Next

    'Open Powerpoint with Powerpoint is already opened
    Set PowerPointApp = GetObject(class:="PowerPoint.Application")

    'Clear errors
    Err.Clear

    'If Powerpoint is closed, open Powerpoint
    If PowerPointApp Is Nothing Then
        Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
    End If

    'Handle error if Powerpoint isn't installed or not found
    If Err.Number = 429 Then
        MsgBox ("PowerPoint not found, aborting...")
        Exit Sub
    End If

On Error GoTo 0

'Make Powerpoint visible and active
PowerPointApp.Visible = True
PowerPointApp.Activate

'Open Powerpoint Presentation from PATH and set it as the active
PowerPointApp.Presentations.Open ("File.pptx")

For Each sld In ActivePresentation.Slides
    For Each shp in sld
       'Iterate through charts and change data of chart using something like If sld Has.Chart Then ...
Next sld

Exit Sub

我在想,也许是因为 ActivePresentation,但我尝试引用 myPresentation,但结果是一样的。

你能帮忙吗?

我明确声明了一个 Presentation 变量来代替 ActivePresentation,这似乎可以解决问题。仅供参考:我还更改了 sldshp 变量的声明以显式引用 PowerPoint 对象库。

Sub pptDataChange()

'Define variables of excel
Dim mySheet As Excel.Worksheet

'Define variables to open on PPT
Dim PowerPointApp As PowerPoint.Application
Dim myPresentation As PowerPoint.Presentation
Dim sld As PowerPoint.Slide
Dim shp As PowerPoint.Shape
Dim chrt As Chart

'Copy range from Excel
Set mySheet = ThisWorkbook.Worksheets("Sheet1")

'Create instance of Powerpoint
On Error Resume Next

    'Open Powerpoint with Powerpoint is already opened
    Set PowerPointApp = GetObject(class:="PowerPoint.Application")

    'Clear errors
    Err.Clear

    'If Powerpoint is closed, open Powerpoint
    If PowerPointApp Is Nothing Then
        Set PowerPointApp = CreateObject(class:="PowerPoint.Application")
    End If

    'Handle error if Powerpoint isn't installed or not found
    If Err.Number = 429 Then
        MsgBox ("PowerPoint not found, aborting...")
        Exit Sub
    End If

On Error GoTo 0

'Make Powerpoint visible and active
PowerPointApp.Visible = True
PowerPointApp.Activate

'Open Powerpoint Presentation from PATH and set it as the active
Dim pres As PowerPoint.Presentation
Set pres = PowerPointApp.Presentations.Open("File.pptx")

For Each sld In pres.Slides
    For Each shp In sld.Shapes
       'Iterate through charts and change data of chart using something like If sld Has.Chart Then ...
    Next shp
Next sld

End Sub