F8 (Step Into) Runs Macro but F5 generates error

时间:2019-04-23 15:18:41

标签: vba

I have a Macro to prepare a presentation using some data from excel and following a template. When I run the macro through F8 it gives me expected result but the same code runs into a run time error "Run-time error '-2147188160(80048240)': Shapes.Item : Integer out of range. 4 is not in Index's valid range of 1 to 3." when macro is run through F5.

One of the line in my code "ppapp.CommandBars.ExecuteMso ("PasteExcelTableSourceFormatting")" adds a shape to slide, but ppt is not counting this shape when the code is running in one go but works fine when step into code line by line.

I have given a skeleton of the entire code in short.

Tried updating all tables one by one and then tried formatting them one by one but did not work. Given code is updating one table and formatting it, then moving to other slide.


Sub PPT2()

 Application.ScreenUpdating = False
 Application.DisplayAlerts = False

    Dim a As Integer

    Dim b As Integer

    Dim ppapp As PowerPoint.Application

    Dim ppPres As PowerPoint.Presentation

    Dim X As Integer

    Dim Y As Integer

    Dim filename As String

    Dim myShape As Object



Set ppapp = CreateObject("PowerPoint.Application")



Set pptpres = ppapp.Presentations.Open(ThisWorkbook.Path & "\Template1.pptx")



filename = ThisWorkbook.Path & "\" & Tabledata1.Range("A1") & Format(Date, "YYYYMMDD") & ".pptx"

pptpres.SaveAs filename

pptpres.Slides(1).Select

pptpres.Slides(1).Shapes(1).TextFrame.TextRange = Tabledata1.Range("A1")

pptpres.Slides(1).Shapes(4).TextFrame.TextRange = Tabledata1.Range("C1")



a = pptpres.Slides(3).Shapes.Count



pptpres.Slides(3).Shapes(1).TextFrame.TextRange = Tabledata1.Range("A2")

Tabledata1.Range("A3:D10").Copy

pptpres.Slides(3).Select

ppapp.CommandBars.ExecuteMso ("PasteExcelTableSourceFormatting")

pptpres.Slides(3).Shapes(4).Height = 150

pptpres.Slides(3).Shapes(4).Width = 600

pptpres.Slides(3).Shapes(4).Top = 150







Tabledata1.Range("A3:D10").Copy

pptpres.Slides(4).Select

ppapp.CommandBars.ExecuteMso ("PasteExcelTableSourceFormatting")

pptpres.Slides(4).Shapes(4).Height = 150

pptpres.Slides(4).Shapes(4).Width = 600

pptpres.Slides(4).Shapes(4).Top = 150







Tabledata1.Range("A3:D10").Copy

pptpres.Slides(5).Select

ppapp.CommandBars.ExecuteMso ("PasteExcelTableSourceFormatting")

pptpres.Slides(5).Shapes(4).Height = 150

pptpres.Slides(5).Shapes(4).Width = 600

pptpres.Slides(5).Shapes(4).Top = 150







Tabledata1.Range("A3:D10").Copy

pptpres.Slides(6).Select

ppapp.CommandBars.ExecuteMso ("PasteExcelTableSourceFormatting")

pptpres.Slides(6).Shapes(4).Height = 150

pptpres.Slides(6).Shapes(4).Width = 600

pptpres.Slides(6).Shapes(4).Top = 150



pptpres.Save

pptpres.Application.Quit



End Sub

0 个答案:

没有答案