• Insert Image in Powerpoint Through Code (2002)

    Home » Forums » AskWoody support » Productivity software by function » MS PowerPoint and presentation apps » Insert Image in Powerpoint Through Code (2002)

    • This topic has 9 replies, 3 voices, and was last updated 21 years ago.
    Author
    Topic
    #403642

    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?

    Viewing 3 reply threads
    Author
    Replies
    • #816491

      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:

      ActivePresentation.Slides.InsertFromFile _
         FileName:="I:GDocsPracticeSlideWPicture.ppt", Index:=1

      -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

      • #816833

        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:

        Set gSlide = gPowerPoint.ActivePresentation.Slides.Add(gPowerPoint.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
        

        Thanks again for the suggestion.

        • #817210

          Jake,

          [indent]


          The images I am inserted are generated at run-time by the program.


          [/indent]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. thumbup

          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

          • #817354

            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.

        • #817211

          Jake,

          [indent]


          The images I am inserted are generated at run-time by the program.


          [/indent]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. thumbup

          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

      • #816834

        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:

        Set gSlide = gPowerPoint.ActivePresentation.Slides.Add(gPowerPoint.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
        

        Thanks again for the suggestion.

    • #816492

      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:

      ActivePresentation.Slides.InsertFromFile _
         FileName:="I:GDocsPracticeSlideWPicture.ppt", Index:=1

      -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

    • #824399

      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

      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
    • #824400

      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

      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
    Viewing 3 reply threads
    Reply To: Reply #816834 in Insert Image in Powerpoint Through Code (2002)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information:




    Cancel