vba 使用 Access 中的图表创建 Powerpoint

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1040720/
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 10:31:16  来源:igfitidea点击:

Creating a Powerpoint with Graphs from Access

ms-accessvbagraphpowerpointpowerpoint-vba

提问by TimothyAWiseman

I am trying to programmatically create a PowerPoint from graphs in Access. Ideally, when the graphs move over to PowerPoint they will become static pictures and not graphs still linked to the access data.

我正在尝试从 Access 中的图形以编程方式创建 PowerPoint。理想情况下,当图表移动到 PowerPoint 时,它们将变成静态图片,而不是仍然链接到访问数据的图表。

I have tried procedures such as:

我尝试过以下程序:

 Private Sub Command1_click()
     Dim pwrpnt as Object
     Dim Presentation as Object

     set pwrpnt = CreateObject("Powerpoint.Application")
     pwrpnt.Activate
     Set Presentation = pwrpnt.Presentation.Open("C:\test.ppt")
     Me.Graph1.SetFocus
     Runcommand acCmdcopy

     Presentation.Slides(1).Shapes.Paste
     set pwrpnt = Nothing
     set Presentation = Nothing
End Sub

And I get an error message such as: Paste method failed.

我收到一条错误消息,例如:粘贴方法失败。

Is there a better approach? And can it be forced to become a static image?

有没有更好的方法?并且可以强制变成静态图像吗?

Thank you.

谢谢你。

回答by TimothyAWiseman

Ok, I found a way to do it. I am still interested if anyone has a more elegant way, but for anyone else dealing with a similar problem:

好的,我找到了一种方法。如果有人有更优雅的方式,我仍然很感兴趣,但对于其他处理类似问题的人:

Private Sub Command1_click()
 'Note: Sample only, in real code this should probably have something to save the 
 'PPT file and then close the powerpoint application, not to mention some error handling,
 ' and possibly some picture formatting, etc.  

 Dim pwrpnt as PowerPoint.Application
 Dim Presntation as PowerPoint.Presentation

 Me.Graph0.Action = acOLECopy
 set pwrpnt = CreateObject("Powerpoint.Application")
 pwrpnt.Activate
 Set Presentation = pwrpnt.Presentations.Open("TemplateFile.ppt")
 pwrpnt.ActiveWindow.ViewType = ppViewSlide

 'This inserts it as a picture, just use .Paste to insert it as an actual chart.
 pwrpnt.ActiveWindow.View.PasteSpecial ppPasteEnhancedMetafile 
EndSub

回答by Chris

Yep, this is exactly the technique I use - and I spent days searching the web for solutions. Its not very elegant but it works and the nice thing is that you get an MS Graph object in Powerpoint so that users can easily apply their own styling , templates etc

是的,这正是我使用的技术 - 我花了几天时间在网上搜索解决方案。它不是很优雅,但它可以工作,好处是您可以在 Powerpoint 中获得一个 MS Graph 对象,以便用户可以轻松地应用他们自己的样式、模板等。