|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
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. |
#2
|
|||
|
|||
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 Last edited by JohnWilson; 04-24-2012 at 01:09 AM. |
#3
|
|||
|
|||
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! |
#4
|
|||
|
|||
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 |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
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 |