如何使用Powerpoint VBA来操作嵌入式excel工作簿?

时间:2018-04-08 03:01:37

标签: vba excel-vba powerpoint excel

我有一个包含100个图表的PowerPoint卡片组。每个图表都包含一个嵌入的Excel文件。打开每个嵌入的Excel文件并从其他地方获取数据以粘贴到其中将需要永远。因此,我正在努力实现这一过程的自动化。(考虑到所有研究和咨询公司每天手动完成这项工作,它肯定具有一定的经济价值。)

  1. 使用PowerPoint VBA,我的代码能够从外部Excel文件复制范围数据并粘贴到PowerPoint中的嵌入式Excel文件中。但是,我无法控制粘贴的位置,现在它是随机发生的;我希望粘贴范围从范围(“A1”)开始。
  2. screenshot

    -

    Sub UpdateChartData3()
        Dim shp As Shape
        Set shp = ActivePresentation.Slides(1).Shapes(1)
        Dim cht As Chart
        Set cht = shp.Chart
        Dim wb As Excel.Workbook
        Dim ws As Excel.Worksheet 
        'This clear all the data in the embedded excel file for the chart(1)
        '    in the first slide.
        Set wb = cht.ChartData.Workbook
        Set ws = wb.Worksheets(1)
        ws.Cells.Clear
        Dim Databook As Excel.Application
        Set Databook = New Excel.Application
        Databook.Visible = True 
        'This opens up an external excel file. Copy a range from the Excel
        '    file and paste into the embedded excel file
        Databook.Workbooks.Open "C:\Folder\Master Slide.xlsx", True, False
        ActiveWorkbook.Worksheets("Sheet1").Range("A1").Select
        Selection.CurrentRegion.Copy
        wb.Activate
        Ws.Paste
        Set Databook = Nothing
    
    1. 真正的问题是当您从powerpoint打开Microsoft Excel对象库时。 Excel和Powerpoint只在最低级别相互通信;这也发生在使用Excel VBA时。我的研究表明,为了实现powerpoint中图表的嵌入式工作簿,面向对象的编程流程应该是:
    2. PowerPoint.presentations.slides.shapes.chart.chartdata.workbook.worksheet.range

      因此,从逻辑上讲,如果我将代码Ws.paste(从上面的编码中)更改为下面的内容,它应该可以正常工作,但事实并非如此。

      ws.Range("A1").selection
      Selection.paste
      'This is to expand the source data table of the chart, so that the chart
      '    in Powerpoint can be automated.
      Dim rng As Range
      Set rng = Range("A1").CurrentRegion
      ws.ListObjects("Table1").Resize ws.rng
      

      真正的问题是缺少PowerPoint VBA文档。有人可以帮忙吗?

0 个答案:

没有答案