从excel中插入后在powerpoint中调整图表(Excel VBA)

问题描述:

我有一些代码需要中的图表并将其粘贴到幻灯片中。但是,一旦它被粘贴到我不知道如何调整图表大小。我研究过查找哪个数字是“形状”,我​​可以手动执行此操作,但我不知道如何自动执行此操作。任何帮助将不胜感激,谢谢!从excel中插入后在powerpoint中调整图表(Excel VBA)

这里是我的代码:

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 

试试这个:

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 
+0

嗯,这似乎不工作。此外,您的代码似乎是针对图表的位置(我已经拥有),而不是图表的大小。 – Andrew

+0

使用.width和.height – nutsch

+0

什么似乎不起作用?你有错误信息吗? – nutsch

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