So the problem I got is explained as followed:
I've written a macro, where a presentation will be created from excel file with a click command (button). I need to insert 2 pictures from this excel file to a powerpoint slide. Both pictures should be separated with a blank in between.
This is the code section I've written:
Sub InteractGenerator()
Application.ScreenUpdating = False
Dim i As Integer, wsCnt As Long
'Boolean for tables and pictures
Dim tableFinder As Boolean, picFinder As Boolean
tableFinder = False
picFinder = False
'Count the Worksheets
wsCnt = ThisWorkbook.Worksheets.Count
Dim mainWb As Workbook
Dim graphsWs As Worksheet
For pptC = 1 To 4
DestinationPPT = Application.ActiveWorkbook.Path & "\AL_PPT_Template.pptx"
Set PowerPointApp = CreateObject("PowerPoint.Application")
'Create a New Presentation
Set myPresentation = PowerPointApp.Presentations.Open(DestinationPPT)
Set mainWb = ThisWorkbook
For i = 1 To wsCnt
If tableFinder = False And picFinder = True Then
Dim oPPtShp As Shape
For Each oPPtShp In ActiveSheet.Shapes
'Needed to be added at the sheet in a range: path of picture is in A13
With oPPtShp
PowerPointApp.ActivePresentation.slides(i - 1).Shapes.AddPicture Range("A13").Value, msoFalse, msoTrue, _
.Left, .Top, .Width, .Height
DoEvents
End With
If mainWb.ActiveSheet.Index = 18 And i = 18 Then
'That´s for the slides which 2 pictures
'Here is the blank needed, the code inserts the picture from "A15" on the picture before
'The same problem is in the other if-condition
With oPPtShp
PowerPointApp.ActivePresentation.slides(i - 1).Shapes.AddPicture Range("A15").Value, msoFalse, msoTrue, _
.Left, .Top, .Width, .Height
Application.Wait Now + TimeSerial(0, 0, 1)
DoEvents
End With
ElseIf mainWb.ActiveSheet.Index = 30 And i = 30 Then
With oPPtShp
PowerPointApp.ActivePresentation.slides(i - 1).Shapes.AddPicture Range("A15").Value, msoFalse, msoTrue, _
.Left, .Top, .Width, .Height
Application.Wait Now + TimeSerial(0, 0, 1)
DoEvents
End With
End If
Debug.Print (i)
Exit For
Next oPPtShp
End If ' I Believe, This End If Was Missing From Your Code
Next i
Next pptC
Application.ScreenUpdating = True
MsgBox "Done!"
End Sub
How can I insert the second Image after the first one as they are shown on the second screenshot below? Both pictures are in one worksheet, and they shouldn't be twice in one powerpoint slide. There are different pictures in different worksheets. It works if there is only one picture in one worksheet, then it gets easily copied to the presentation slides.
Edit:
What I am getting now is this:
What I need is this:
... and if I comment out Exit For
(the 8th line, counting backwards from the end) as it has been suggested by someone, then this is what happens:
Left
andTop
properties to different positions on the slide. Do a bit of calculation, like taking the height of the first picture and adding something to that to get theTop
position for the next one. – Cindy MeisterActiveWorkbook
, but even then I would only use it at the initial stage of my code to assign that Workbook to an Object Reference Variable likeSet WB = ActiveWorkbook
(withDim WB as Workbooks
before it). After that I would only ever refer toWB
in my code insted of usingActiveWorkbook
, likeWB.Worksheets(1)
– AndrasActiveWorksheet
in my code if I was operating within the boundaries of that one Worksheet, and even then like above, only by tying it to a reference variable:Set Sht = ActiveWorkbook.ActiveWorksheet
(certainly preceding it withDim Sht as Worksheet
). Then I would just refer to it in my code asSht.Range("A1")
. ------- Referring to Worksheets and Workbooks this way is Safe and Simple. – Andras