vba 用于在ppt幻灯片中插入图表的VBA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1019864/
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
VBA for Inserting a chart into a ppt slide
提问by Justin
Below is a procedure that I use to pull charts into a ppt from excel spreadsheets. However one thing I cannot figure out is how to insert the picture into the "object" instead of just pasting it onto th screen. (ie if I did a ppLayoutFourObjects, and sent fours charts to this slide, before adding another, I need to know how to paste the chart into each designated rectangle shown from the 4 Objects selection). I know that the first one seems to always be rectangle five, I can't get the code right. Please help. This is all 2003 Office.
下面是我用来从 Excel 电子表格中将图表拉入 ppt 的过程。然而,我无法弄清楚的一件事是如何将图片插入“对象”而不是仅仅将其粘贴到屏幕上。(即,如果我做了一个 ppLayoutFourObjects,并且将四张图表发送到这张幻灯片,在添加另一个之前,我需要知道如何将图表粘贴到从 4 个对象选择中显示的每个指定矩形中)。我知道第一个似乎总是矩形五,我无法正确获取代码。请帮忙。这都是2003 Office。
sub xls2ppt()
'I use this to pull charts into ppt from excel
Dim xlApp As Object
Dim xlWrkBook As Object
Dim lCurrSlide As Long
Set xlApp = CreateObject("Excel.Application")
' Open the Excel workbook
Set xlWrkBook = xlApp.Workbooks.Open("X:\Users\Admin\Desktop\Budget Overview.xls")
' Copy picture of the 1st chart object onto the clipboard
xlWrkBook.Worksheets(2).ChartObjects(1).CopyPicture
' Get the slide number
lCurrSlide = ActiveWindow.Selection.SlideRange.SlideNumber
' Paste the picture onto the PowerPoint slide.
ActivePresentation.Slides(lCurrSlide).Shapes.Paste
' Close the open workbook without saving changes
xlWrkBook.Close (False)
xlApp.Quit
Set xlApp = Nothing
Set xlWrkBook = Nothing
End Sub
Thanks for any help. VBA for PowerPoint is my weakest, but I am really in need to pick it up for work! Thanks guys!
谢谢你的帮助。VBA for PowerPoint 是我最弱的,但我真的需要拿起它来工作!谢谢你们!
回答by Gary McGill
AFAIK you can't paste a chart "into an object" in PowerPoint, even through the UI. In Word, you can paste into a textbox or into a table cell, but not in PowerPoint.
AFAIK 您无法将图表粘贴到 PowerPoint 中的“对象”中,即使通过 UI 也是如此。在 Word 中,您可以粘贴到文本框或表格单元格中,但不能粘贴到 PowerPoint 中。
What you need to do instead is position the 4 pasted charts so that they're the right size & position - and that's easy enough to do...
您需要做的是定位 4 个粘贴的图表,使它们的大小和位置正确 - 这很容易做到......
Set oSlide = ActivePresentation.Slides(lCurrSlide)
Set oShape = oSlide.Shapes.Paste
oShape.Top = 10
oShape.Left = 10
oShape.Width = 100
oShape.Height = 100