Manage OLE Objects in Presentations in .NET

Introduction

Object Linking and Embedding (OLE) lets a PowerPoint slide contain an object created in another application, such as a chart from an Excel workbook. An embedded object stores its data in the presentation. A linked object refers to an external file.

PowerPoint can display the object as an icon or show a preview of its contents. Double-clicking the object activates it for editing when the associated application is available. Aspose.Slides for .NET represents both embedded and linked objects as an OleObjectFrame.

Add OLE Object Frames to Slides

To embed an Excel workbook with a chart, pass the workbook bytes and the xlsx extension to OleEmbeddedDataInfo. The extension identifies the file type so PowerPoint can select the associated application. In this example, AddOleObjectFrame places the workbook on the first slide and sizes the frame to cover the slide.

using System.IO;
using Aspose.Slides;
using Aspose.Slides.DOM.Ole;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];
var slideSize = presentation.SlideSize.Size;

var bytes = File.ReadAllBytes("book.xlsx");
var data = new OleEmbeddedDataInfo(bytes, "xlsx");
slide.Shapes.AddOleObjectFrame(0, 0, slideSize.Width, slideSize.Height, data);

presentation.Save("output.pptx", SaveFormat.Pptx);

Add Linked OLE Object Frames

The linked overload of AddOleObjectFrame takes an OLE class identifier and the path to the source file. Here, Excel.Sheet.12 identifies an Excel workbook. The example places a 200 × 150-point frame 20 points from the top and left edges of the first slide. The workbook must remain accessible at the stored path for PowerPoint to open it.

using System.IO;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var workbookPath = Path.GetFullPath("book.xlsx");
slide.Shapes.AddOleObjectFrame(20, 20, 200, 150, "Excel.Sheet.12", workbookPath);

presentation.Save("output.pptx", SaveFormat.Pptx);

Access OLE Object Frames

Use EmbeddedData to read the file stored in an embedded OLE frame. This example inspects the first shape on the first slide and prints the file extension and byte count when embedded data is available.

using System;
using Aspose.Slides;

using var presentation = new Presentation("sample.pptx");
var shapes = presentation.Slides[0].Shapes;

if (shapes.Count == 0 || shapes[0] is not IOleObjectFrame frame || frame.IsObjectLink)
{
    Console.WriteLine("The first shape is not an embedded OLE object.");
    return;
}

var data = frame.EmbeddedData;
if (data?.EmbeddedFileData is not { Length: > 0 } bytes)
{
    Console.WriteLine("The frame contains no embedded file data.");
    return;
}

Console.WriteLine($"Embedded file extension: {data.EmbeddedFileExtension}, size: {bytes.Length} bytes.");

Access Linked OLE Object Frame Properties

For a linked frame, IsObjectLink is true and LinkPathLong identifies the source file. The example prints the stored path for the first shape on the first slide. A PPT presentation can also store a relative path in LinkPathRelative, which the example prints when available.

using System;
using Aspose.Slides;

using var presentation = new Presentation("sample.ppt");
var shapes = presentation.Slides[0].Shapes;

if (shapes.Count == 0 || shapes[0] is not IOleObjectFrame frame || !frame.IsObjectLink)
{
    Console.WriteLine("The first shape is not a linked OLE object.");
    return;
}

Console.WriteLine($"Linked file: {frame.LinkPathLong}");
if (!string.IsNullOrEmpty(frame.LinkPathRelative))
{
    Console.WriteLine($"Relative path: {frame.LinkPathRelative}");
}

Change OLE Object Data

The example expects an embedded XLSX workbook in the first shape on the first slide. It opens the workbook from memory, writes a label and three values to cells E1:E4 on the first worksheet, and saves the workbook back to memory in XLSX format. SetEmbeddedData replaces the data stored in the frame so the edited cells are available when the embedded workbook is opened.

using System;
using System.IO;
using Aspose.Slides;
using Aspose.Slides.DOM.Ole;
using Aspose.Slides.Export;

using var presentation = new Presentation("sample.pptx");
var shapes = presentation.Slides[0].Shapes;

if (shapes.Count == 0 || shapes[0] is not IOleObjectFrame frame || frame.IsObjectLink)
{
    Console.WriteLine("The first shape is not an embedded OLE object.");
    return;
}

var data = frame.EmbeddedData;
if (data?.EmbeddedFileData is not { Length: > 0 } bytes)
{
    Console.WriteLine("The frame contains no embedded file data.");
    return;
}

if (!string.Equals(data.EmbeddedFileExtension?.TrimStart('.'), "xlsx", StringComparison.OrdinalIgnoreCase))
{
    Console.WriteLine("The embedded file is not an XLSX workbook.");
    return;
}

using var input = new MemoryStream(bytes);
using var workbook = new Aspose.Cells.Workbook(input);
var cells = workbook.Worksheets[0].Cells;
cells["E1"].PutValue("E");
cells["E2"].PutValue(12);
cells["E3"].PutValue(14);
cells["E4"].PutValue(15);

using var output = new MemoryStream();
workbook.Save(output, Aspose.Cells.SaveFormat.Xlsx);
var updatedBytes = output.ToArray();
var updatedData = new OleEmbeddedDataInfo(updatedBytes, "xlsx");
frame.SetEmbeddedData(updatedData);

presentation.Save("output.pptx", SaveFormat.Pptx);

Embed Other File Types in Slides

You can also embed HTML documents, PDFs, and ZIP archives. This example uses AddOleObjectFrame to place an HTML document and a ZIP archive in separate frames on the first slide. Setting IsObjectIcon to true displays each file as an icon that the reader can double-click to open in an associated application.

using System.IO;
using Aspose.Slides;
using Aspose.Slides.DOM.Ole;
using Aspose.Slides.Export;

using var presentation = new Presentation();
var slide = presentation.Slides[0];

var htmlBytes = File.ReadAllBytes("sample.html");
var htmlData = new OleEmbeddedDataInfo(htmlBytes, "html");
var htmlFrame = slide.Shapes.AddOleObjectFrame(150, 120, 50, 50, htmlData);
htmlFrame.IsObjectIcon = true;

var zipBytes = File.ReadAllBytes("sample.zip");
var zipData = new OleEmbeddedDataInfo(zipBytes, "zip");
var zipFrame = slide.Shapes.AddOleObjectFrame(150, 220, 50, 50, zipData);
zipFrame.IsObjectIcon = true;

presentation.Save("output.pptx", SaveFormat.Pptx);

Set File Types for Embedded Objects

The extension in OleEmbeddedDataInfo describes the format of the supplied bytes. If the first shape on the first slide already contains a ZIP archive with an incorrect extension, use SetEmbeddedData to correct its type. The example prints the current extension and saves the same archive bytes with the zip extension.

using System;
using Aspose.Slides;
using Aspose.Slides.DOM.Ole;
using Aspose.Slides.Export;

using var presentation = new Presentation("sample.pptx");
var shapes = presentation.Slides[0].Shapes;

if (shapes.Count == 0 || shapes[0] is not IOleObjectFrame frame || frame.IsObjectLink)
{
    Console.WriteLine("The first shape is not an embedded OLE object.");
    return;
}

var data = frame.EmbeddedData;
if (data?.EmbeddedFileData is not { Length: > 0 } bytes)
{
    Console.WriteLine("The frame contains no embedded file data.");
    return;
}

Console.WriteLine($"Current embedded file extension: {data.EmbeddedFileExtension}");
var archiveData = new OleEmbeddedDataInfo(bytes, "zip");
frame.SetEmbeddedData(archiveData);

presentation.Save("output.pptx", SaveFormat.Pptx);

Set Icon Images and Titles for Embedded Objects

To customize the icon for an embedded object, assign an image from the presentation’s image collection to SubstitutePictureFormat and set the caption with SubstitutePictureTitle. The example uses a PNG image for the first shape on the first slide and enables IsObjectIcon to display the icon and caption.

using System;
using System.IO;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation("sample.pptx");
var shapes = presentation.Slides[0].Shapes;

if (shapes.Count == 0 || shapes[0] is not IOleObjectFrame frame || frame.IsObjectLink)
{
    Console.WriteLine("The first shape is not an embedded OLE object.");
    return;
}

var imageBytes = File.ReadAllBytes("image.png");
var iconImage = presentation.Images.AddImage(imageBytes);
frame.IsObjectIcon = true;
frame.SubstitutePictureFormat.Picture.Image = iconImage;
frame.SubstitutePictureTitle = "Embedded workbook";

presentation.Save("output.pptx", SaveFormat.Pptx);

Prevent an OLE Object Frame from Being Resized and Repositioned

When PowerPoint refreshes a linked object’s preview, the frame can move or change size. Set UpdateAutomatic to false to request manual updates for that link. The example applies this setting to the first shape on the first slide when it is a linked OLE frame.

using System;
using Aspose.Slides;
using Aspose.Slides.Export;

using var presentation = new Presentation("sample.pptx");
var shapes = presentation.Slides[0].Shapes;

if (shapes.Count == 0 || shapes[0] is not IOleObjectFrame frame || !frame.IsObjectLink)
{
    Console.WriteLine("The first shape is not a linked OLE object.");
    return;
}

frame.UpdateAutomatic = false;

presentation.Save("output.pptx", SaveFormat.Pptx);

Extract Embedded Files

Read EmbeddedFileData to extract the original bytes and EmbeddedFileExtension to name the output. This example scans the top-level shapes on the first slide, skips linked objects, and writes each embedded file to the current directory. The shape index distinguishes files with the same extension.

using System;
using System.IO;
using Aspose.Slides;

using var presentation = new Presentation("sample.pptx");
var shapes = presentation.Slides[0].Shapes;

var extracted = 0;
for (var index = 0; index < shapes.Count; index++)
{
    if (shapes[index] is not IOleObjectFrame frame || frame.IsObjectLink)
        continue;

    var data = frame.EmbeddedData;
    if (data?.EmbeddedFileData is not { Length: > 0 } bytes)
    {
        Console.WriteLine($"Shape {index} contains no embedded file data.");
        continue;
    }

    var extension = data.EmbeddedFileExtension?.TrimStart('.');
    var path = Path.ChangeExtension($"OLE_object_{index}", extension);
    File.WriteAllBytes(path, bytes);
    extracted++;
}

Console.WriteLine($"Extracted {extracted} embedded files.");

List Embedded and Linked OLE Objects

The ObjectProgId property identifies the OLE object type associated with an application. For example, Word.Document.12 identifies a Word document. The embedded file extension is a separate value. The ProgID alone cannot confirm the format or validity of the stored data.

The example scans every slide in sample.pptx and follows IGroupShape.Shapes into nested groups. For each IOleObjectFrame, it prints the slide number, shape name, IsObjectLink, and the original ProgID. An unfamiliar ProgID appears as stored. An empty ProgID leaves the last column blank. The example reads only metadata, without extracting, opening, or activating the OLE content.

using System;
using Aspose.Slides;

using var presentation = new Presentation("sample.pptx");

Console.WriteLine("Slide | Shape | IsObjectLink | ObjectProgId");
foreach (var slide in presentation.Slides)
{
    ReportObjects(slide.Shapes, slide.SlideNumber);
}

static void ReportObjects(IShapeCollection shapes, int slideNumber)
{
    foreach (var shape in shapes)
    {
        if (shape is IOleObjectFrame frame)
        {
            Console.WriteLine($"{slideNumber} | {shape.Name} | {frame.IsObjectLink} | {frame.ObjectProgId}");
        }
        else if (shape is IGroupShape group)
        {
            ReportObjects(group.Shapes, slideNumber);
        }
    }
}

FAQ

What appears when I export an OLE object to PDF or an image?

The export shows the icon or preview image visible on the slide. To control its appearance, set a custom preview before exporting. To include the embedded file as a PDF attachment, enable PdfOptions.IncludeOleData. This option is disabled by default. See Preserve Embedded OLE Files as PDF Attachments.

How can I prevent accidental changes to an OLE frame in PowerPoint?

Apply shape-level locks to restrict actions such as moving or resizing the frame.

Why does a linked Excel object move or change size when I open the presentation?

PowerPoint may refresh the preview using the dimensions of the linked worksheet range. To keep the dimensions consistent, fit the frame to the range or scale the range to a fixed frame and provide a matching preview. See Working Solution for Worksheet Resizing.

Can a PPTX presentation store a relative path for a linked OLE object?

The relative path is available only in the older PPT format. For PPTX, use a full path that recipients can access or embed the file in the presentation.