vba 如何打开现有 PowerPoint 图表的数据表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12825131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 18:04:34  来源:igfitidea点击:

How to Open Datasheet for Existing PowerPoint Chart

excel-vbapowerpoint-vbavbaexcel

提问by user1617979

I have searched extesively and do not seem to find an answer. I want to update the data in an existing powerpoint chart. So far I am able to open he presentation,select the page, but I am not able to go past the last line below.

我已经广泛搜索,似乎没有找到答案。我想更新现有 powerpoint 图表中的数据。到目前为止,我能够打开他的演示文稿,选择页面,但我无法越过下面的最后一行。

I need to open the datasheet so I can paste the data there. I was able to do it with a new chart but then the dataseheet opens immediately, but I want to use an existign chart as it will have all formatting as required. I will then do this for multiple charts in multiple pages to update a standard presetation for a new group ofcustomers.

我需要打开数据表,以便将数据粘贴到那里。我可以用一个新图表来做,但是数据表会立即打开,但我想使用一个存在的图表,因为它会根据需要进行所有格式设置。然后,我将对多个页面中的多个图表执行此操作,以更新一组新客户的标准预设。

Can someone show how would I open the datashet so I can select the right cell and paste my new data (whihc omes from Excel). For many reasons I do not want to pasteexcel charts,I want the daa to reside in PowerPoint. This macro is run from the excel file that has the new data.

有人可以展示我将如何打开数据表,以便我可以选择正确的单元格并粘贴我的新数据(来自 Excel)。出于多种原因,我不想粘贴excel图表,我希望 daa 驻留在 PowerPoint 中。该宏从包含新数据的 excel 文件中运行。

Sub test2()

Set rngData = Range("A1:D6")

Set ObjPPT = CreateObject("PowerPoint.Application")
Set ObjPresentation = ObjPPT.Presentations.Open("C:\Documents and Settings\ewnym5s\My Documents\Document_sample.pptx")
Set ObjSlide = ObjPresentation.Slides(1)
Set mychart = ObjSlide.Shapes("Chart 1").Chart
mychart.Select

Set wb = mychart.ChartData
Set ws = wb.Worksheets(1)

回答by Daniel

To open the datasheet you have to select the shape, and then activate the chartdata. And then you can access the workbook. I've modified your code slightly with some comments

要打开数据表,您必须选择形状,然后激活图表数据。然后您可以访问工作簿。我用一些注释稍微修改了你的代码

  Sub test2()

    Set rngData = Range("A1:D6")

    Set ObjPPT = CreateObject("PowerPoint.Application")
    Set ObjPresentation = ObjPPT.Presentations.Open("C:\Documents and Settings\ewnym5s\My Documents\Document_sample.pptx")
    Set ObjSlide = ObjPresentation.Slides(1)
    Set mychart = ObjSlide.Shapes("Chart 1").Chart
    mychart.Select

    'Activate the chart data
    mychart.ChartData.Activate
    'Retrieve the workbook.
    Set wb = mychart.ChartData.Workbook
    Set ws = wb.Worksheets(1)

    'Do stuff with the workbook then close with save
    wb.Close True
  end Sub

Incase you do not know this, when you activate the chartdata Excel will open to display the workbook. There is no way to avoid having it at least flicker if you use this method. To make the flicker as brief as possible you could add wb.parent.Visible = false. If you cannot stand the flicker, I indicated a possible work around in this thread, but it requires the use of OLEObjects and older controls.

如果您不知道这一点,当您激活图表数据时,Excel 将打开以显示工作簿。如果您使用此方法,则无法避免它至少闪烁。为了使闪烁尽可能简短,您可以添加wb.parent.Visible = false. 如果你不能忍受闪烁,我在这个线程中指出了一个可能的解决方法,但它需要使用 OLEObjects 和旧控件。