Insert OLE Objects in Word, Excel, PowerPoint with C#

OLE stands for Object Linking and Embedding. It is provided by Microsoft and allows you to create and edit documents containing items or objects that are created by various applications.

As an example, you can embed spreadsheets, images, and sound clips as OLE objects in a Word document. You can use these OLE objects in the Word document and do not worry about switching to multiple applications again and again. You can embed or insert such objects programmatically using OLE in C#.

This article will guide you about how you can:

Steps in this article and code samples are using GroupDocs.Merger for .NET. So please make sure to install the API from any of the following methods:

  • Install using NuGet Package Manager.
  • Download the DLL and reference it into the project.

Insert PDF as OLE Object into MS Word Document in C#

Insert PDF as OLE in Word Document in C#

Here are the steps and C# code sample to show how to embed a PDF file into a Word document as an OLE Object:

  1. Instantiate the OleWordProcessingOptions with embedding options and the document to embed in a Word document.
  2. Now instantiate the Merger object with the source Word document path or stream.
  3. Call the ImportDocument method and pass the object of OLE Word Processing Options that are set in step 1.
  4. That’s it. Call the Save method to get the resultant Word document having a PDF document as an OLE object.
// Embed a PDF file into a Word document as an OLE Object in C#
int pageNumber = 2;
OleWordProcessingOptions oleWordProcessingOptions = new OleWordProcessingOptions(@"embedded-doc.pdf", pageNumber)
{ 
    Width = 300, // Just setting the height & width, you have more options.
    Height = 300
};
// Use Merger class to start with source Word document and embed PDF as OLE object.
using (Merger merger = new Merger(@"source-doc.docx"))
{
    merger.ImportDocument(oleWordProcessingOptions);
    merger.Save(@"word-document-with-OLE.docx");
}

Insert Word Document as OLE Object into Excel Spreadsheet in C#

Insert Word File s OLE in Excel Spreadsheet in C#

We can embed OLE objects into Excel spreadsheets. CSharp Code sample and steps below explaining  how to add a Word document into an Excel spreadsheet as an OLE Object:

  1. Instantiate the OleSpreadsheetOptions with embedding options and the document to embed in an Excel spreadsheet.
  2. Now instantiate the Merger object with the source Spreadsheet path or stream.
  3. Now call the ImportDocument method and pass the object of OLE Spreadsheet Options that are set in step 1.
  4. Finally, call the Save method to get the resultant Excel Spreadsheet having a Word document as an OLE object.
// Embed a Word file into an Excel Spreadsheet as an OLE Object in C#
int pageNumber = 2;
OleSpreadsheetOptions oleSpreadsheetOptions = new OleSpreadsheetOptions(@"embedded-doc.docx", pageNumber)
{
    RowIndex = 2, // Setting the Row & height Index, you have more options.
    ColumnIndex = 2
};
// Using Merger class with source spreadsheet and embedding a Word document as an OLE object.
using (Merger merger = new Merger(@"sample-doc.xlsx"))
{
    merger.ImportDocument(oleSpreadsheetOptions);
    merger.Save(@"excel-sheet-with-ole.xlsx");
}

Add PDF as OLE Object to PowerPoint Presentation in C#

Insert PDF as OLE in PowerPoint Presentation in C#

Similarly, here we are inserting objects in a PowerPoint presentation.

  1. Instantiate the OlePresentationOptions with embedding options and the document to embed in a PowerPoint presentation.
  2. Now instantiate the Merger object with the source Presentation path or stream.
  3. Call the ImportDocument method and pass the object of OLE Presentation Options that are set in step 1.
  4. Finally, call the Save method to get the resultant PowerPoint presentation with a PDF document as an OLE object.
// Embed a PDF file into an Excel Spreadsheet as an OLE Object in C#
int pageNumber = 2;
OlePresentationOptions olePresentationOptions = new OlePresentationOptions(@"embedded.pdf", pageNumber)
{
    X = 10, // Setting only X & Y coordinates, you can customize more.
    Y = 10
};
// Using Merger class to embed a PDF file as an OLE object in the PowerPoint presentation.
using (Merger merger = new Merger(@"sample-presentation.ppt"))
{
    merger.ImportDocument(olePresentationOptions);
    merger.Save(@"powerpoint-presentation-with-ole.ppt");
}

Conclusion

We have discussed how easy and quickly we can insert OLE Objects in Word, Excel, or PowerPoint documents programmatically in C#. There is only a small difference in code for each objective i.e. different OLE options class and its options for each file format:

  • OleWordProcessingOptions to embed OLE objects in a Word document.
  • OleSpreadsheetOptions to embed OLE objects in Excel Spreadsheets.
  • OlePresentationOptions to embed OLE objects in PowerPoint presentation.

You can learn more about the API from the documentation or Let’s talk more @ Free Support Forum.