Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-23-2012, 02:02 PM
CatMan CatMan is offline Need help to insert a picture (.jpg) into slide 1 of an existing PowerPoint Windows 7 32bit Need help to insert a picture (.jpg) into slide 1 of an existing PowerPoint Office 2010 32bit
Intermediate
Need help to insert a picture (.jpg) into slide 1 of an existing PowerPoint
 
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
  #2  
Old 04-23-2012, 11:02 PM
JohnWilson JohnWilson is offline Need help to insert a picture (.jpg) into slide 1 of an existing PowerPoint Windows 7 64bit Need help to insert a picture (.jpg) into slide 1 of an existing PowerPoint Office 2010 32bit
Programmer
 
Join Date: Nov 2008
Location: UK
Posts: 1,912
JohnWilson has a spectacular aura aboutJohnWilson has a spectacular aura about
Default

Why do you declare oPA as the PPT app and then use ppt??

oPSe??

Main problem though is you haven't set a reference to either the new presentation or the slide.

Note I have changed the paths to test.

Code:
Option Explicit

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
Dim ExistingPPT As String
Dim MyPicture As String
'Set path and fielname of existing PowerPoint
ExistingPPT = "C:\Users\John\Desktop\Test.pptx" 'make sure presentation has one blank slide.
 
'Set path and filename of picture to be imported into ppt
MyPicture = "C:\Users\John\Desktop\Test.jpg"
 
'Open Existing PowerPoint
Set oPA = CreateObject("PowerPoint.Application")
With oPA
    'Open existing PowerPoint (THIS BLOCK WORKS)
    .Visible = True
Set oPP = .Presentations.Open(ExistingPPT)
    .ActiveWindow.View.GotoSlide (1) 'this line makes testing easier otherwise not required
Set oPS = oPP.Slides(1)
   '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(oPS.Shapes.Count)
'The next two line are not needed using -1 (True) when adding already did this
   ' 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
__________________
Microsoft PowerPoint MVP 2007-2023
Free Advanced PowerPoint Tips and Tutorials

Last edited by JohnWilson; 04-24-2012 at 01:09 AM.
Reply With Quote
  #3  
Old 04-24-2012, 12:46 PM
CatMan CatMan is offline Need help to insert a picture (.jpg) into slide 1 of an existing PowerPoint Windows 7 32bit Need help to insert a picture (.jpg) into slide 1 of an existing PowerPoint Office 2010 32bit
Intermediate
Need help to insert a picture (.jpg) into slide 1 of an existing PowerPoint
 
Join Date: Apr 2012
Posts: 39
CatMan is on a distinguished road
Default

Hi JohnWilson,

Many thanks again for your excellent help, I was really stuck on this one. I must remember to "declare, set, with" for each object I operate on. Setting a reference for oPA and oPP did the trick, well after I removed that erroneous reference to ppt. And thanks for the heads up to remove those two scaleHeight/ScaleWidth lines. Thank you again, and long live this forum and VBA!
Reply With Quote
  #4  
Old 04-24-2012, 01:25 PM
JohnWilson JohnWilson is offline Need help to insert a picture (.jpg) into slide 1 of an existing PowerPoint Windows 7 64bit Need help to insert a picture (.jpg) into slide 1 of an existing PowerPoint Office 2010 32bit
Programmer
 
Join Date: Nov 2008
Location: UK
Posts: 1,912
JohnWilson has a spectacular aura aboutJohnWilson has a spectacular aura about
Default

You're welcome.

There's nothing wrong with your method for centering the shape and if you follow my stuff you'll know I keep saying selecting shapes in code is usually a bad idea!

It can help here though because you can say (if you had selected when you paste).

Code:
With ActiveWindow.Selection.ShapeRange
.Align msoAlignCenters, msoTrue
.Align msoAlignMiddles, msoTrue
End With
__________________
Microsoft PowerPoint MVP 2007-2023
Free Advanced PowerPoint Tips and Tutorials
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help to insert a picture (.jpg) into slide 1 of an existing PowerPoint How to insert hidden chapter markers (headings) into existing Word outline? GKent Word 4 04-16-2012 08:52 AM
Powerpoint automatically changing picture size when adding a picture (2010) One_Life PowerPoint 7 01-20-2012 06:57 AM
PowerPoint 2003 - Change default location for Insert Picture Fee PowerPoint 0 05-12-2011 02:15 AM
How to insert a reference to an existing endnote dwschulze Word 0 01-23-2010 08:59 AM
Apply design template to existing slide mellowyellow PowerPoint 0 01-26-2009 12:59 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:18 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft