2013-02-22 29 views
1

我一直在使用excel vba将excel文件中的图表复制并粘贴到使用下面的代码的ppt文件中。这是我尝试过的代码另一个应用程序它工作得很好,但我在另一个应用程序中使用相同的代码,它会抛出错误“应用程序定义的或对象定义的错误”,同时将chart1粘贴到ppt中。任何人都可以告诉我我在哪里做错了,需要做些什么改变。“应用程序定义或对象定义的错误”从Excel复制图表到Powerpoint

Sub PasteToPPT(FileName As String) 
Dim file As String 
file = FileName 

Dim pptPres As PowerPoint.Presentation 
Dim AppPPT As PowerPoint.Application 
Dim SlidePPT As PowerPoint.Slide 
Dim cht As Excel.ChartObject 
Dim Sht As Excel.Sheets 

Set AppPPT = CreateObject("PowerPoint.Application") 
    AppPPT.Visible = True 

    AppPPT.Presentations.Open (file) 

    'AppPPT.Presentations.Open FileName:=file 

    Excel.Sheets("Charts").Activate 

    AppPPT.ActiveWindow.View.GotoSlide 1 
     Set SlidePPT = AppPPT.ActivePresentation.Slides(1) 

    Excel.Sheets("Charts").ChartObjects("Chart1").Copy '<-- Here i'm getting the error 
     AppPPT.ActivePresentation.Slides(1).Shapes.Paste.Select 

    AppPPT.ActiveWindow.Selection.ShapeRange.LockAspectRatio = False 

    AppPPT.ActiveWindow.Selection.ShapeRange.Left = 0 
    AppPPT.ActiveWindow.Selection.ShapeRange.Top = 275 

    AppPPT.ActiveWindow.Selection.ShapeRange.Width = 966 
    AppPPT.ActiveWindow.Selection.ShapeRange.Height = 200 

    Excel.Sheets("Charts").ChartObjects("Chart2").Copy 
     AppPPT.ActivePresentation.Slides(1).Shapes.Paste.Select 

    AppPPT.ActiveWindow.Selection.ShapeRange.LockAspectRatio = False 

    AppPPT.ActiveWindow.Selection.ShapeRange.Left = 0 
    AppPPT.ActiveWindow.Selection.ShapeRange.Top = 390 

    AppPPT.ActiveWindow.Selection.ShapeRange.Width = 966 
    AppPPT.ActiveWindow.Selection.ShapeRange.Height = 200 

    'AppPPT.ActivePresentation.SaveAs ("D:\Projects\IEB MBU MYR US\Demo_Slide.pptx") 
    AppPPT.ActivePresentation.Save 

    Set SlidePPT = Nothing 
    ''AppPPT.Quit 
    Set AppPPT = Nothing 

End Sub 

在此先感谢。

+0

没有你的工作簿,很难调试。你确定图表的名字是“图表1”而不是“图表1”? – 2013-02-22 10:07:00

+0

我已将Chart 1修改为chart1。 – 2013-02-22 10:30:00

+0

仍然是同样的问题? – 2013-02-22 10:35:05

回答

0

从迪内希的评论,这里是万一有人回答绊倒在它以后:

Sheets("Chart1")必须Worksheets("Chart1")更换。

Sheets集合只包含真正的“计算”工作表 - 但Worksheet集合另外包含图表工作表(和Excel4宏工作表)。由于Chart1是一个图表,它不包括在Sheets集合中......

相关问题