View Single Post
 
Old 04-23-2012, 11:02 PM
JohnWilson JohnWilson is offline Windows 7 64bit Office 2010 32bit
Programmer
 
Join Date: Nov 2008
Location: UK
Posts: 1,914
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