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))
Else
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!
Dave