Free Support Forum -

Adding pictures to PPT via VBA

I have a problem adding pictures to a PPT via VBA. My project is a little complicated so bear with me on the explanation:

I have an Excel spreadsheet that contains data, charts, etc. There is VBA code in the the spreadsheet that uses PPT to create a presentation from data in the Excel spreadsheet. I am trying to eliminate the need for PowerPoint by using Aspose.Slides instead. I wrote a .NET (2.0) library that allows the VBA code to add "commands" to a command processor which does things like clone slides, insert text and, for this problem, insert pictures into the PPT. The picture I am inserting is actually an image of a chart that is in the Excel spreadsheet. The VBA code looks something like this:

Set ipCommand = New InsertPictureCommand
ipCommand.Description = ""
ipCommand.Height = dataRow.Height
ipCommand.Left = dataRow.Left
ipCommand.TargetSlide = dataRow.DestinationSlide
ipCommand.Top = dataRow.Top
ipCommand.Width = dataRow.Width
ipCommand.NewName = dataRow.NewName

Dim chart As Excel.ChartObject
Dim ws As Worksheet
Set ws = reportWorkbook.Worksheets(dataRow.Source)
Set chart = ws.ChartObjects(1)
chart.CopyPicture xlScreen, xlBitmap
Dim oPic As IPicture
Set oPic = PastePicture(xlBitmap)

ipCommand.SetImage oPic

InsertPictureCommand is an object in my .NET library. The important part is where get a reference to the Excel chart and then call CopyPicture to export an image of the chart to the Windows clipboard. I then use a third-party component that allows me to paste the clipboard image into an object of type IPicture (which I believe is stdole.IPicture). Finally I call the SetIPicture method of the .NET object to marshall the image to a .NET Image object. The code for SetIPicture is below:

Public Sub SetIPicture(ByVal image As Object)

_sourceImage = Microsoft.VisualBasic.Compatibility.VB6.IPictureToImage(image)

End Sub

Notice that I use a method from the Microsoft.VisualBasic.Compatibility namespace to convert the IPicture object to a .NET Image object (_sourceImage is of type Image). Finally I use Aspose.Slides to insert the image into a PowerPoint file:

If (TypeOf _sourceImage Is Bitmap) Then

pic = New Aspose.Slides.Picture(target.PowerPoint, CType(_sourceImage, Bitmap))


pic = New Aspose.Slides.Picture(target.PowerPoint, CType(_sourceImage, Metafile))

End If

When I open the PPT file I get the message:

Some controls on this presentation can't be activated. They might not be registered on this computer.

and there are not images in the PPT. This leads me to think that the image is not being inserted into the PPT as an image but rather as some OLE object. Here's the really interesting part. If I save the image to disk in the call to SetIPicture I can view the image! So, this tells me that the image is actually being passed from COM to .NET with no problem. So why then when I call AddPictureFrame it causes the above error to be generated?

Please help!


Dear Dave,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Did you try adding the picture from a disk after saving it there or also make sure that your code of adding picture resembles to code here.

Yes, I did try saving the image to disk and then loading it on the .NET side. I used the Excel Chart object's Export method to save the image as a PNG file. I have attached one of the images.

My code closely resembles the sample code you mention.

I have got no problem in adding the picture you provided. Below is my code and you can also see the output presentation attached with this post.

Dim pres As New Presentation

Dim sld As Slide = pres.GetSlideByPosition(1)

'Create a picture object

Dim pic As Aspose.Slides.Picture = New Aspose.Slides.Picture(pres, "d:\source\tmp4F6.png")

'Add the picture into pictures collection and save its id

Dim picId As Integer = pres.Pictures.Add(pic)

'Add the picture frame for the given id

sld.Shapes.AddPictureFrame(picId, 100, 100, 3000, 3000)


I must apologize because I now realize that the problem was not with the charts. I was able to import them properly. The problem is that a slide I am trying to copy from one presentation to another contains a Shockwave Flash Object. It appears that this object does copy with the slide but this is what is generating the error. Is it a problem copying a slide that contains a Flash object?