简体   繁体   English

使用Excel VBA编辑PowerPoint数据表图表对象

[英]Edit PowerPoint datasheet chart object with Excel VBA

I have a PowerPoint presentation that I need to update through Excel VBA and I am currently stuck at adding data to the datasheet within a chart. 我有一个PowerPoint演示文稿,需要通过Excel VBA更新,而我目前仍坚持在图表中向数据表中添加数据。 Below the code. 下面的代码。 What this should do is open the PowerPoint presentation through Excel VBA and assuming the Excel is open, take the range from there and paste it in the DataChart. 这应该做的是通过Excel VBA打开PowerPoint演示文稿,并假定Excel已打开,从那里获取范围并将其粘贴到DataChart中。

I'm still fairly new to objects, more so to PowerPoint objects and I can't figure out how to paste it there. 我对对象还是很陌生,对PowerPoint对象更是如此,我不知道如何将其粘贴到那里。 The object is a msoEmbeddedOLEObject and the OLEFormat.progID is "MSGraph.Chart.8" which I sadly do not understand. 该对象是一个msoEmbeddedOLEObject,而OLEFormat.progID是“ MSGraph.Chart.8”,我很难过。

Public sPath As String, sFile As String, sFilePPT As String

Public PPApp As PowerPoint.Application
Public PPPres As PowerPoint.Presentation
Public PPSlide As PowerPoint.Slide
Public PPShape As PowerPoint.Shape
Public PPChart As PowerPoint.Chart
Public PPChartData As PowerPoint.ChartData
Public cTable As Excel.ListObject


Sub OpenPPT()

sPath = ThisWorkbook.Path & "\"
sFilePPT = "Presentation1.pptx"

On Error Resume Next
'==> Check if PowerPoint is running
    Set PPApp = GetObject(, "PowerPoint.Application") 
    If PPApp Is Nothing Then
'==> If PowerPoint is not running, create new instance
        Set PPApp = CreateObject("PowerPoint.Application") 
'==> and make it visible (PowerPoint must be visible to be used)
        PPApp.Visible = True 
        Set PPPres = PPApp.Presentations.Open(sPath & sFilePPT)
    End If
On Error GoTo 0

'==> Reference presentation and slide
On Error Resume Next 
'==> If there's at least one presentation, use it
    If PPApp.Windows.Count > 0 Then 
        Set PPPres = PPApp.ActivePresentation
 '==> use active slide
        Set PPSlide = PPPres.Slides(PPApp.ActiveWindow.Selection.SlideRange.SlideIndex) 
    Else
        MsgBox "PowerPoint Presentation not found"
        Exit Sub
    End If
On Error GoTo 0

Set PPSlide = Nothing
Set PPPres = Nothing
Set PPApp = Nothing

End Sub


Sub test()

Dim i As Byte
Dim r As Range

Call OpenPPT

Set PPApp = GetObject(, "PowerPoint.Application")
Set PPPres = PPApp.Presentations(1)
Debug.Print PPPres.Name
Set PPSlide = PPPres.Slides(2)
PPSlide.Select
Debug.Print PPSlide.Name
Set PPShape = PPSlide.Shapes(2)
PPShape.Select

If PPShape.OLEFormat.progID = "MSGraph.Chart.8" Then 
    Set r = Workbooks("Budget_CM11.xlsm").Worksheets("Recap").Range("AQ12:AY17")
    r.Copy
'==> I see it opens the DataChart of the Chart for editing
    PPShape.OLEFormat.DoVerb 2 

'code needed here that should copy the Excel range 
'within the PowerPoint Object (Chart?) Data 

End If



End Sub

The only answer I've found was to manually convert the charts in the presentation to a newer format. 我发现的唯一答案是将演示文稿中的图表手动转换为较新的格式。 Now the data table can be addressed, but I find it a bit finicky as it creates an Excel instance in PowerPoint. 现在可以处理数据表了,但是我发现它有点麻烦,因为它在PowerPoint中创建了Excel实例。 I'm not sure it's the most efficient, but it works. 我不确定这是否是最有效的方法,但是它确实有效。 The code to open the PowerPoint presentation remains unchanged. 打开PowerPoint演示文稿的代码保持不变。

Below the code: 代码下方:

Option Explicit

Public sPath As String, sFile As String, sFilePPT As String

Public PPApp As PowerPoint.Application
Public PPPres As PowerPoint.Presentation
Public PPSlide As PowerPoint.Slide
Public PPShape As PowerPoint.Shape
Public PPChart As PowerPoint.Chart
Public PPChartData As PowerPoint.ChartData

Sub test()
Application.ScreenUpdating = False

Dim i As Byte
Dim r As Range
Dim wb As Workbook
Dim ws As Worksheet

Call OpenPPT

Set PPApp = GetObject(, "PowerPoint.Application")
Set PPPres = PPApp.Presentations(1)
Set PPSlide = PPPres.Slides(2)
Debug.Print PPSlide.Name
Set PPShape = PPSlide.Shapes(2)
Set PPChart = PPShape.Chart
Set PPChartData = PPChart.ChartData
PPChartData.Activate
Set wb = PPChartData.Workbook
Set ws = wb.Worksheets(1)

Set r = Workbooks("Budget_CM11.xlsm").Worksheets("RECAP").Range("AQ12:AY17")
r.Copy
ws.Range("B2:J7").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
wb.Close True
PPChart.Select

Application.ScreenUpdating = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM