简体   繁体   English

运行时错误9:下标超出范围(粘贴时出错)

[英]Run time error 9: subscript out of range (error while pasting)

I'm relatively new to VBA. 我是VBA的新手。 I'm trying out the following VBA code but its throwing an error: 'Runtime error 09: subscript out of range'. 我正在尝试以下VBA代码,但会引发错误:“运行时错误09:下标超出范围”。 This error occurs when i'm trying the paste operation in Graph 1 section of the code.. 当我尝试代码图1部分中的粘贴操作时,会发生此错误。

can someone help with figuring out as to where i'm going wrong. 有人可以帮忙弄清楚我要去哪里错了。 I have declared the presentation/slide etc. still i'm facing this problem.. 我已经声明了演示文稿/幻灯片等。我仍然面临着这个问题。

Sub UK()

Dim oPPTApp As PowerPoint.Application
 Dim oPPTFile As PowerPoint.Presentation
 Dim oPPTShape As PowerPoint.Shape
 Dim oPPTSlide As PowerPoint.Slide
 Dim SlideNum As Integer
 Dim mycells As Range
 Set oPPTApp = CreateObject("PowerPoint.Application")
 srcdir = "D:\WBR\Week 2"
 srcfile = srcdir & "\" & Dir(srcdir + "\*.pptx")
 Set oPPTFile = oPPTApp.Presentations.Open(srcfile)
 Set oPPTSlide = oPPTFile.Slides(2)


' for graph 1
 Set oPPTShape = oPPTFile.Slides(2).Shapes("Picture 3") 
 oPPTShape.Delete

 ThisWorkbook.Sheets("New Charts").Activate
 Sheets("New Charts").Shapes.Range(Array("Group 21")).Select
 Selection.CopyPicture

 oPPTApp.ActivePresentation.Slides(2).Select 
 Set Picture = oPPTSlide.Shapes.Paste
 Picture.Name = "Picture 3" 

With oPPTApp.ActivePresentation.Slides(2).Shapes("Picture 3") 
  .Top = Application.InchesToPoints(3)
  .Left = Application.InchesToPoints(0.22)
End With

If I understand you correctly, you want to: 如果我对您的理解正确,则希望:

  • Open a saved presentation 打开保存的演示文稿
  • Delete "Picture 3" from Slide 2 从幻灯片2中删除“图片3”
  • Copy Chart/Range from your excel sheet 从Excel工作表复制图表/范围
  • Paste it in Slide 2 将其粘贴到幻灯片2中
  • Name it as "Picture 3" 将其命名为“ Picture 3”
  • Set it's position on the slide 设置它在幻灯片上的位置

Well the below code does exactly that: 下面的代码可以做到这一点:

'Make Sure to load the PowerPoint Object Library
'Tools ---> References ---> Microsoft PowerPoint xx.x Object Library

    Dim pptApp As PowerPoint.Application
    Dim pptPres As PowerPoint.Presentation
    Dim pptSlide As PowerPoint.Slide
    Dim objChart As Chart

    Set pptApp = New PowerPoint.Application

    'presentation path here
    srcdir = "C:\"
    Set pptPres = pptApp.Presentations.Open(srcdir & "Presentation" & ".pptx")

    Set pptSlide = pptPres.Slides(2)

    For j = 1 To pptSlide.Shapes.Count
        With pptSlide.Shapes(j)
    If .Name = "Picture 3" Then
    .Delete
    End If
        End With
    Next j

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    'Change "Chart 1" to the name of your chart if you are copying a chart
    Worksheets("New Charts").ChartObjects("Chart 1").Activate
    Set objChart = Worksheets("New Charts").ChartObjects("Chart 1").Chart
    objChart.CopyPicture

    'If you are copying a range of cells then use
    Worksheets("New Charts").Range("A1:A10").Copy

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Set MyPic = pptSlide.Shapes.PasteSpecial(DataType:=ppPasteEnhancedMetafile)

    With MyPic
    .Name = "Picture 3"
    End With

    With pptSlide.Shapes("Picture 3")
    .Top = Application.InchesToPoints(3)
    .Left = Application.InchesToPoints(0.22)
    End With

    'use this line to set focus to slide 2 if you want to
    pptPres.Slides(2).Select

    pptPres.Save 'use this line to save if you want to

    Set pptSlide = Nothing
    Set pptPres = Nothing
    Set pptApp = Nothing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM