首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >线条图的Powerpoint循环. Active X错误

线条图的Powerpoint循环. Active X错误
EN

Stack Overflow用户
提问于 2015-06-24 08:52:52
回答 1查看 153关注 0票数 0

我编写了一个代码来打开excel中的Powerpoint,然后循环遍历所有的幻灯片,找到图表并更改一些列。我有做替换的代码,但是不能“循环”幻灯片,因为它抛出了一个ActiveX错误429,也就是说没有找到powerpoint :O。

代码语言:javascript
复制
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,但它是一样的。

你能帮忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-24 16:31:41

我显式地声明了要使用的Presentation变量而不是ActivePresentation,这似乎起到了作用。FYI:我还更改了sldshp变量的声明,以显式引用PowerPoint对象库。

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31021996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档