View Single Post
 
Old 04-23-2012, 02:02 PM
CatMan CatMan is offline Windows 7 32bit Office 2010 32bit
Intermediate
 
Join Date: Apr 2012
Posts: 39
CatMan is on a distinguished road
Default Need help to insert a picture (.jpg) into slide 1 of an existing PowerPoint

Hello forum and thanks to all for taking the time to look at this.

I am trying to insert (addpicture) a .jpg picture into slide 1 of an existing PowerPoint file using Excel VBA (Excel is host). I am getting run-time error 91, "Object variable or with block variable not set.". I have included code to resize and center the picture on the slide, I believe these two blocks work but I cannot test it until the addpicture method is solved. I have tried to set various objects to solve the problem but I am not clear which 'with block' is missing.

Here is a much simplified version of my code, the line that is failing is marked "Add Picture": (The code must run in an Excel module)

Code:
Sub TestCode()
'Declare
Dim oPA As PowerPoint.Application
Dim oPP As PowerPoint.Presentation
Dim oPS As PowerPoint.Slide
Dim oShape As PowerPoint.Shape
Dim oPicture As PowerPoint.Shape
 
'Set path and fielname of existing PowerPoint
'ExistingPPT = "Libraries\Documents\Test.pptx" 'make sure presentation has one blank slide.
 
'Set path and filename of picture to be imported into ppt
MyPicture = "Libraries\Documents\Tara1.jpg"
 
'Open Existing PowerPoint
Set ppt = CreateObject("PowerPoint.Application")
With ppt
    'Open existing PowerPoint (THIS BLOCK WORKS)
    .Visible = True
    .Presentations.Open (existingPPT)
    .ActiveWindow.View.GotoSlide (1) 'this line makes testing easier otherwise not required
 
   'Add Picture (e.g. import it into PPT) (THIS BLOCK DOES NOT WORK)
    Set oShape = oPS.Shapes.AddPicture(MyPicture, msoFalse, msoTrue, 1, 1, -1, -1) '(Filename, LinkToFile, SaveWithDocument, Left, Top, Width, Height)
 
   'Size Picture
    Set oPicture = oPS.Shapes(oPSe.Shapes.Count)
    oPicture.ScaleHeight 1, msoTrue
    oPicture.ScaleWidth 1, msoTrue
    oPicture.LockAspectRatio = True
    oPicture.Width = 750 '750 is arbitrary
 
   'Center picture
    With oPA.ActivePresentation.PageSetup
        oPicture.Left = (.SlideWidth \ 2) - (oPicture.Width \ 2)
        oPicture.Top = (.SlideHeight \ 2) - (oPicture.Height \ 2)
    End With
End With
 
End Sub

Last edited by CatMan; 04-23-2012 at 03:03 PM.
Reply With Quote