Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Apr 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Insert Image in Powerpoint Through Code (2002)

    I've been banging my head against this one for some time and hope someone here might be able to help me.

    When you create a new "Title and Content" slide, the lower rectangle says "Click icon to add content." If you click the icon for a picture and insert a picture, it replaces the lower rectangle with that picture. Then the picture is resized and moved depending on whatever the currently selected Master design is.

    I want to be able to do this through VB/VBA code, but I cannot find a way to do it. Even if I record a macro performing these exact actions, the macro will not recreate it exactly. The macro only inserts a picture, it does not replace the actual content rectangle and do whatever is necessary so that the picture will automatically reformat with design changes.

    Does anyone have any clues on how this all works?

  2. #2
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Insert Image in Powerpoint Through Code (2002)

    Jake,

    This seems to be a nasty aspect of doing VBA in PPT: the functionality of the placeholders, while available in the user interface, is not programmable with VBA (apparently).

    The following approach gets around the problem; would it be an option in your case?:

    Rather than attempt to insert and build the slide programatically, create a separate PPT file to serve as your slide source, and insert the picture manually as you like it. You can then use code to insert this slide into your new presentation, along the lines of:

    <pre>ActivePresentation.Slides.InsertFromFile _
    FileName:="I:GDocsPracticeSlideWPicture.ppt", Index:=1</pre>

    -obviously your specific path would differ.
    - I don't have the PowerPoint VBA Help available here to check, but I think the Index property is used to specify where in the new presentation, the new slide will be inserted. There are also optional SlideStart and SlideEnd properties, which I think can be used to pull a specific slide from a source file which contained more than one slide (so for example, you could have one "SlidesSource" file, keep all your pre-built slides in there, and pull the specific slides into your current presentation as needed).

    Gary

  3. #3
    New Lounger
    Join Date
    Apr 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Insert Image in Powerpoint Through Code (2002)

    Thank you Gary, but that won't be possible. The images I am inserted are generated at run-time by the program.

    However, I found a suitable workaround. I insert a LayoutObject slide, then set the fill of "Rectangle 3" to my image and resize it to the proper dimensions. Something like this:

    <pre>Set gSlide = gPowerPoint.ActivePresentation.Slides.Add(gPowerPo int.ActivePresentation.Slides.Count + 1, ppLayoutObject)
    With gSlide.Shapes("Rectangle 3")
    .Fill.Visible = msoTrue
    .Fill.ForeColor.RGB = RGB(255, 255, 255)
    .Fill.BackColor.SchemeColor = ppBackground
    .Fill.UserPicture strImageLocation
    .Width = iImageWidth
    .Height = iImageHeight
    .Left = (gSlide.Master.Width - .Width) / 2
    End With
    </pre>


    Thanks again for the suggestion.

  4. #4
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Insert Image in Powerpoint Through Code (2002)

    Jake,

    <hr>The images I am inserted are generated at run-time by the program.
    <hr>
    Sounds like an interesting application - from the 'gPowerPoint' I gather you're automating PowerPoint from VB or something? Just curious - what is the program for?

    Anyway, glad you came up with a solution. <img src=/S/thumbup.gif border=0 alt=thumbup width=15 height=15>

    One little glitch when I trying running (an adapted version of) this: the picture gets inserted in the new slide, but the little rectangle with the six 'Insert' options (Insert Table, Insert Chart etc.) still remains visible, on top of the picture - do you get that as well?

    Gary

  5. #5
    New Lounger
    Join Date
    Apr 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Insert Image in Powerpoint Through Code (2002)

    Yes, I'm using VB to automate PowerPoint. I am writing output "plugins" for my company's application. These plugins take our application's data and create various files from it, such as a Word document or a Powepoint slideshow. I got the idea of using the .Fill property of the rectangle from code I found on some newsgroup posting.

    The icons still remain visible in the picture when using Normal view. However, they are not visible in slideshow view. I guess that has to be good enough for me.

  6. #6
    Silver Lounger
    Join Date
    Mar 2001
    Location
    Springfield, Ohio, USA
    Posts
    2,136
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Insert Image in Powerpoint Through Code (2002)

    I finally went to work and grabbed a sample of this sort of stuff. It runs from an Excel macro rather than VB, but you can get an idea of how I solved a similar problem. I have attached a zip file with all of the files & instructions, but here is the code that I used. HTH --Sam
    <pre>Option Explicit

    Sub PastePics()
    ' You must add a reference to PowerPoint Object Library
    Dim i As Integer, sngL As Single, sngT As Single
    Dim ws As Worksheet
    Dim ppApp As New PowerPoint.Application
    Dim ppShapes As PowerPoint.Shapes
    Set ws = Worksheets("Pics")
    For i = 2 To ws.UsedRange.Rows.Count
    ' ppApp.Visible = True

    With ppApp.Presentations.Open( _
    Filename:=ActiveWorkbook.Path & "" & ws.Cells(i, 1), _
    WithWindow:=False)
    If ws.Cells(i, 3) = "M" Then
    Set ppShapes = .SlideMaster.Shapes
    Else
    Set ppShapes = .Slides(ws.Cells(i, 3).Value).Shapes
    End If
    sngL = Application.InchesToPoints(ws.Cells(i, 4))
    sngT = Application.InchesToPoints(ws.Cells(i, 5))
    With ppShapes.AddPicture( _
    Filename:=ActiveWorkbook.Path & "" & ws.Cells(i, 2), _
    LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, _
    Left:=sngL, Top:=sngT)
    If ws.Cells(i, 6) = "Yes" Then _
    .PictureFormat.ColorType = msoPictureWatermark
    End With
    .Save
    .Close
    End With
    Next i
    Set ppShapes = Nothing
    Set ppApp = Nothing
    End Sub</pre>

    <font face="Comic Sans MS">Sam Barrett, CACI </font face=comic>
    <small>And the things that you have heard... commit these to faithful men who will be able to teach others also. 2 Timothy 2:2</small>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •