So, I am writing this code that is supposed to loop through several tables in Excel and paste it into PowerPoint. This process works fine. But, after pasting the table as a range I am trying to change the dimensions of the shape (#7 in the case of my slides). However, at this point the code Set PPShape = PPSlide.Shapes(7)
and gives me the following error "Method 'Item' of object 'Shapes' failed (see image)
So, it does not recognize the shape that it just copied into PowerPoint as a shape.
However, once I click debug and run the code again without changing a single line of code it works. VBA then does detect shape #7 on the respective slide and changes its dimensions to the given parameters. I have tried to write the process of selecting the shape and resizing it in a different subroutine, and then call this subroutine from within the original one. No luck. And, On Error also does not seem to get past this bug. Anybody got an idea of how to fix this?
Option Explicit
Sub DBible()
'Declaring all necessary PowerPoint variables
Dim PP As PowerPoint.Application
Dim PPPres As PowerPoint.Presentation
Dim PPSlide As PowerPoint.Slide
Dim PPShape As PowerPoint.Shape
Dim OriginFile As String 'File path to PowerPoint template
'Declaring all necessary Excek-l variables
Dim adminSh As Worksheet 'Sheet containing the data to be exported
Dim configRng As Range 'Selection of cells used to mark the exported ranges
Dim rng As Range 'Each individual cell within range
Dim vRange$ 'Cell address
Dim expRng As Range 'Range to be exported
'Dimension variables to resize and position exported shapes
Dim vWidth As Double
Dim vHeight As Double
Dim vTop As Double
Dim vLeft As Double
Dim vSlide_No As Double 'Slide in which range will be pasted
'Create and open PowerPoint
Set PP = CreateObject("PowerPoint.Application")
OriginFile = "https://'CompanyName-my.sharepoint.com/personal/CompanyName/Documents/Documents/PLS%20DO%20NOT%20TOUCH%20-%20BRP%20ORIGIN.pptx?web=1"
PP.Presentations.Open (OriginFile)
PP.Visible = True
PP.WindowState = ppWindowMaximized
Set PPPres = PP.ActivePresentation
'Select sheet where data will be exported from and define the origin of all the ranges
Set adminSh = Sheets("PERF-LIQ-VEL-BETA")
Set configRng = adminSh.Range("rng_sheets")
'Loop that starts process of copy pasting the ranges from Excel to PowerPoint
For Each rng In configRng
With adminSh 'Defining value of variables or location in which value of variable can be found in the excel
vRange$ = .Cells(rng.Row, 6).Address
vTop = 127
vLeft = 14.2
vWidth = 692.64
vHeight = 235
vSlide_No = .Cells(rng.Row, 4).Value
End With
PPPres.Windows(1).Activate
PPPres.Windows(1).View.GotoSlide (vSlide_No)
Set expRng = Sheets("PERF-LIQ-VEL-BETA").Range(vRange$).CurrentRegion 'Set range from excel
expRng.Copy 'Set range from excel
PP.CommandBars.ExecuteMso "PasteSourceFormatting" 'Paste
Set PPSlide = PPPres.Slides(vSlide_No)
Set PPShape = PPSlide.Shapes(7) 'Set shape to be altered
With PPShape 'Resize and position range
.Top = vTop
.Left = vLeft
.Width = vWidth
.Height = vHeight
End With
'Clear memory to avoid errors in process of copying next range
Set PPShape = Nothing
Set PPSlide = Nothing
Application.CutCopyMode = False
Next rng 'Repeat
End Sub