【问题标题】:Export Excel charts as pictures to PowerPoint将 Excel 图表作为图片导出到 PowerPoint
【发布时间】:2022-01-26 20:59:31
【问题描述】:

我在 excel 工作簿中每个工作表一个图表和多个工作表,我正在尝试将所有图表作为图片导出到 PowerPoints 问题是我当前的代码不起作用 我的代码错误:

  1. PowerPoint 按预期打开

  2. 复制我在运行之前选择的活动表的图表

  3. 复制的图表粘贴到 PowerPoint 中

  4. PowerPoint 创建下一张幻灯片

  5. 现在再次复制来自同一张表的同一张图表的问题!我希望代码移动到下一张表,选择上面的图表,然后在下一张幻灯片中复制过去!

    子 TEST_ExportChartsToPowerPoint_SingleWorkbook() '声明 PowerPoint 对象变量 将 pptApp 调暗为 PowerPoint.Application 将 pptPres 调暗为 PowerPoint.Presentation 将 pptSlide 调暗为 PowerPoint.Slide 将 SldIndex 调暗为整数 '声明 Excel 对象变量 将 Chrt 调暗为 ChartObject '将 Chrt 调暗为 ChartArea 将 WrkSht 调暗为工作表

     'Create a new instance of PowerPoint
     Set pptApp = New PowerPoint.Application
         pptApp.Visible = True
    
     'Create a new Presentation within the PowerPoint Application
     Set pptPres = pptApp.Presentations.Add
    
     'Create an index handler for the slide creation
     SldIndex = 1
    
     'Loop through all of the worksheets in the active work book
     For Each WrkSht In Worksheets
         'WrkSht.Select
    
         ActiveChart.ChartArea.Select
         ActiveChart.ChartArea.Copy
    
         'Create a new slide, set the layout to blank, and paste the chart on the slide
         Set pptSlide = pptPres.Slides.Add(SldIndex, ppLayoutBlank)
         pptSlide.Shapes.Paste
    
         'Increment our slide index
         SldIndex = SldIndex + 1
    
     Next WrkSht
    

    结束子

【问题讨论】:

  • 这里有问题吗?
  • 问题是我当前的代码不起作用
  • 我做了一个快速测试,代码对我有用。请准确描述发生了什么:您是否收到运行时错误? PowerPoint 是否打开?是否创建了幻灯片?活动工作簿是否有一些图表?您是否调试过代码以查看失败的地方?
  • 代替做什么?运行代码时会发生什么? “我的代码不起作用”并不是对实际发生的情况非常有用的描述。
  • 1. PowerPoint 按预期打开 2. 我在运行之前选择的活动工作表的图表被复制 3. 复制的图表被粘贴到 PowerPoint 4. PowerPoint 创建下一张幻灯片 现在问题来自同一张工作表的相同图表被复制再次!我希望代码移动到下一张表,选择上面的图表,然后在下一张幻灯片中复制过去!

标签: excel vba powerpoint excel-charts


【解决方案1】:

问题在于您尝试引用图表的方式。看看你的循环:

'Loop through all of the worksheets in the active work book
    For Each WrkSht In Worksheets
        'WrkSht.Select
    
        ActiveChart.ChartArea.Select
        ActiveChart.ChartArea.Copy
    
        'Create a new slide, set the layout to blank, and paste the chart on the slide
        Set pptSlide = pptPres.Slides.Add(SldIndex, ppLayoutBlank)
        pptSlide.Shapes.Paste
    
        'Increment our slide index
        SldIndex = SldIndex + 1
    
     Next WrkSht

即使您遍历所有Worksheets,您也只能引用ActiveChart。您需要参考循环中工作表上的图表。默认情况下,ActiveChart 引用活动工作表上的图表。这就是为每张幻灯片复制相同图表的原因,因为参考永远不会改变。所以你需要在循环中更改对当前工作表的引用。

更改以下行

ActiveChart.ChartArea.Select
ActiveChart.ChartArea.Copy

WrkSht.ChartObjects(1).Chart.ChartArea.Copy

此代码正确引用了图表。另请注意,您不需要使用ChartArea.Select 方法。

【讨论】:

  • 我已经尝试了您建议的代码行并得到了运行时错误“1004”:对象“_Worksheet”的方法“CharObjects”失败,它突出显示了您建议的代码行,有什么帮助吗?
  • @MohamadAlkhatib “CharObjects”还是“ChartObjects”?您是否在评论或代码中拼错了?
  • 错误是 ChartObject,对不起,我在评论中拼错了,我仔细检查了代码语法是否正确
  • 该错误来自尝试从没有任何图表的工作表中复制图表。由于您正在遍历所有工作表并尝试复制该工作表上的第一个图表,因此每个工作表上至少需要一个图表。
  • 工作表上只有一个图表,因为图表一个一个移到一个新的工作表中,里面除了图表什么都没有
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-01
  • 1970-01-01
  • 2015-10-14
  • 2021-06-16
  • 2014-10-15
  • 2015-07-26
相关资源
最近更新 更多