How to extract OLE objects from PowerPoint documents using Aspose.Slides?

Aspose.Slides is a file management component that can help you read/write/manipulate the PowerPoint documents.

You can also extract OLE objects from PowerPoint documents using Aspose.Slides. For example, I have attached a sample application here along with solution/project in C#, it basically reads the source ppt and extracts the OLE objects (excel chart and pdf document) from it and write it to C: drives.

Hopefully, it will be helpful for you.

You can ask any of your questions regarding Aspose.Slides from the Aspose.Slides forum.

C# Code

//Read the source ppt file
string sourcePath = @"..\..\..\source.ppt";
Presentation srcPres = new Presentation(sourcePath);

//Get the first slide
Slide sld = srcPres.GetSlideByPosition(1);

//Output file id
int fid = 0;

//Extract all ole objects
foreach (Shape shp in sld.Shapes)
{
    //oof will not be null, if shape is ole object frame
    OleObjectFrame oof = shp as OleObjectFrame;

    if (oof != null)
    {
        //Check the class
        string objName = oof.ObjectClassName;

        //decide extension
        //if it is excel, set it as .xls
        //if it is pdf, set it as .pdf
        string ext = "";
        if (objName.Contains("Microsoft Excel Chart") == true)
            ext = ".xls";
        else
        if (objName.Contains("Adobe Acrobat 7.0 Document") == true)
            ext = ".pdf";

        //Now extract the data and write it to file
        FileStream fout = new FileStream("c:\\outOLE" + fid + ext, FileMode.Create, FileAccess.Write);
        fout.Write(oof.ObjectData, 0, oof.ObjectData.Length);
        fout.Close();

        //increment fid
        fid++;
    }
}

What is the equivalent of this line:

OleObjectFrame oof = shp as OleObjectFrame;


in VB.NET?

I’ve tried this:

Dim oof As Aspose.Slides.OleObjectFrame = CType(shape, Aspose.Slides.OleObjectFrame)

but no luck.

I have updated the thread with VB.NET code, which works exactly the same as C# code in previous post.

VB.NET

'Read the source ppt file
Dim sourcePath As String = "..\..\..\source.ppt"
Dim srcPres As Presentation = New Presentation(sourcePath)

'Get the first slide
Dim sld As Slide = srcPres.GetSlideByPosition(1)

'Output file id
Dim fid As Integer = 0

'Extract all ole objects
For Each shp As Shape In sld.Shapes
    If TypeOf shp Is OleObjectFrame Then
        Dim oof As OleObjectFrame = shp

        'Check the class
        Dim objName As String = oof.ObjectClassName

        'Decide the extension
        'if it is excel, set it as .xls
        'if it is pdf, set it as .pdf
        Dim ext As String = ""
        If objName.Contains("Microsoft Excel Chart") = True Then
            ext = ".xls"
        ElseIf objName.Contains("Adobe Acrobat 7.0 Document") = True Then
            ext = ".pdf"
        End If

        'Now extract the data and write it to file
        Dim fout As FileStream
        fout = New FileStream("c:\outOLE" & fid & ext, FileMode.Create, FileAccess.Write)
        fout.Write(oof.ObjectData, 0, oof.ObjectData.Length)
        fout.Close()

        'increment fid
        fid = fid + 1
    End If
Next

I am using .NET 1.0 and this code does not work. This line:

Dim oof As Aspose.Slides.OleObjectFrame = shp

gives me this error: “Value of type ‘Aspose.Slides.Shape’ cannot be converted to ‘Aspose.Slides.OLEObjectFrame’”

Try this one

Dim oof As OleObjectFrame = CType(shp, OleObjectFrame)

If you check my post three messages up, that’s exactly what I tried already. Any other thoughts?

Hello,

Everything should work fine if you didn’t forget to check shape type before this line like shown in the VB example also 3 messages up:

If TypeOf shp Is OleObjectFrame Then
Dim oof As OleObjectFrame = CType(shp, OleObjectFrame)

This is not a run-time error, I am not able to compile my code at all when trying to cast this value.

I am using MDE 2002 v 7.0.9955, .NET 1.0

Figured it out. You have to instantiate shp as a generic object:

Dim shp As Object

That way, you can cast it as anything you like. This is most likely an issue with .NET 1.0

Thanks. We’ll give it a try.