我编写了一个代码来打开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,但它是一样的。
你能帮忙吗?
发布于 2015-06-24 16:31:41
我显式地声明了要使用的Presentation变量而不是ActivePresentation,这似乎起到了作用。FYI:我还更改了sld和shp变量的声明,以显式引用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 Subhttps://stackoverflow.com/questions/31021996
复制相似问题