逻辑:
Chart
获取包含对象的形状
- 获取您的
Chart
对象,然后获取Chartdata
- 启动图表并设置您的工作簿和 Excel 应用程序对象
假设:
出于演示目的,我假设以下内容
Slide1
在演示文稿中有一个形状是图表
- 我已经添加了这两种情况。即
Automating Excel from PowerPoint
和Automating PowerPoint from Excel
这是你正在尝试的吗?
代码:从 PowerPoint 自动化 Excel
Option Explicit
Sub Sample()
'~~> Excel Objects
Dim oXlApp As Object, oXlWb As Object, oXlSheet As Object
'~~~> Powerpoint objects
Dim oPPChart As Chart
Dim oPPChartData As ChartData
'~~> Working with shape1
With ActivePresentation.Slides(1).Shapes(1)
If .HasChart Then
Set oPPChart = .Chart
Set oPPChartData = oPPChart.ChartData
oPPChartData.Activate
'~~> Set your Excel objects here
Set oXlWb = oPPChartData.Workbook
Set oXlApp = oXlWb.Parent
oXlApp.Visible = False
Debug.Print oXlApp.Name
Debug.Print oXlWb.Name
'
'~~> Rest of your code
'
End If
End With
'~~> Close And Cleanup
oXlWb.Close False
oXlApp.Quit
Set oXlSheet = Nothing
Set oXlWb = Nothing
Set oXlApp = Nothing
End Sub
代码:从 Excel 自动化 PowerPoint
Option Explicit
Sub Sample()
'~~> Excel Objects
Dim oXlApp As Application, oXlWb As Workbook, oXlSheet As Worksheet
'~~~> Powerpoint objects
Dim oPPApp As Object, oPPprsn As Object, oPPSlide As Object
Dim oPPChart As Object, oPPChartData As Object
Application.ScreenUpdating = False
'~~> Establish a Popwerpoint application object
On Error Resume Next
Set oPPApp = GetObject(, "PowerPoint.Application")
'~~> If not found then create new instance
If Err.Number <> 0 Then
Set oPPApp = CreateObject("PowerPoint.Application")
End If
Err.Clear
On Error GoTo 0
'~~> open relevant powerpoint file
Set oPPprsn = oPPApp.Presentations.Open("C:\Presentation1.pptx")
Set oPPSlide = oPPprsn.Slides(1)
'~~> Working with shape1
With oPPSlide.Shapes(1)
If .HasChart Then
Set oPPChart = .Chart
Set oPPChartData = oPPChart.ChartData
oPPChartData.Activate
'~~> Set your Excel objects here
Set oXlWb = oPPChartData.Workbook
Set oXlApp = oXlWb.Parent
'~~> This is required if powerpoint chartdata
'~~> opens in the same instance of this excel
oXlWb.Windows(1).Visible = False
'oXlApp.Visible = False
Debug.Print oXlApp.Name
Debug.Print oXlWb.Name
'
'~~> Rest of your code
'
End If
End With
oXlWb.Windows(1).Visible = True
'~~> Close And Cleanup
oXlWb.Close False
oPPprsn.Close
oPPApp.Quit
Application.ScreenUpdating = True
Set oPPChartData = Nothing
Set oPPChart = Nothing
Set oPPChartData = Nothing
Set oPPSlide = Nothing
Set oPPprsn = Nothing
Set oPPApp = Nothing
End Sub