从Excel中插入后,在powerpoint中调整图表大小(Excel VBA)

时间:2012-10-19 23:28:43

标签: excel vba powerpoint

我有一些代码会在中显示图表并将其粘贴到幻灯片中。但是,一旦粘贴到,我不确定如何调整图表的大小。我已经研究过找到哪个数字“形状”,我​​可以手动完成,但我不知道如何自动化。任何帮助将不胜感激,谢谢!

这是我的代码:

Sub AddChartToPPT(PPT As PowerPoint.Application, Slide As Integer, Chart As String, FromTop As Double, FromLeft As Double, Length As Double, Width As Double)
Dim activeslide As PowerPoint.Slide
PPT.ActiveWindow.View.GotoSlide Slide
Set activeslide = PPT.ActivePresentation.Slides(Slide)
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.ChartArea.Copy
activeslide.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture).Select
PPT.ActiveWindow.Selection.ShapeRange.Left = FromLeft
PPT.ActiveWindow.Selection.ShapeRange.Top = FromTop
'Need to add scaling code
End Sub

2 个答案:

答案 0 :(得分:2)

试试这个:

dim shp as Object
set shp=activeslide.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture)
shp.Left = FromLeft
shp.Top = FromTop

所以使用其余的代码:

Sub AddChartToPPT(PPT As PowerPoint.Application, Slide As Integer, Chart As String, FromTop As Double, FromLeft As Double, Length As Double, Width As Double)
Dim activeslide As PowerPoint.Slide, shp as Object
PPT.ActiveWindow.View.GotoSlide Slide
Set activeslide = PPT.ActivePresentation.Slides(Slide)
ActiveSheet.ChartObjects(Chart).Activate
ActiveChart.ChartArea.Copy
set shp=activeslide.Shapes.PasteSpecial(DataType:=ppPasteMetafilePicture)
shp.Left = FromLeft
shp.Top = FromTop
'Need to add scaling code     
End Sub

答案 1 :(得分:2)

根据我的经验,在将图表对象粘贴到PowerPoint之前,可以更轻松地在Excel中调整图表对象的大小。首先,Excel的对象模型更清晰。其次,调整元文件大小可能会导致失真,同时调整图表大小并粘贴为不需要调整大小的元文件也不会。