How to Insert OLE Object in Excel in C#, VB.NET

OLE object is used to make content created in one program available in another program, for instance, we can insert Word as OLE object in Excel sheet.

As a robust component, Spire.XLS supports to insert Word and PowerPoint slide as linked object or embedded object into Excel. In this article, we make an example to explain how to insert Word as OLE object into Excel using Spire.XLS and Spire.Doc. Before coding, you need to download Spire.Office and reference related the Dlls in your VS project.

Code Snippet:

Step 1: Define a GetDocImage(string doxcFile) method to get olePicture. Actually, the olePicture is an image of data information in original Word document. The image generated from the specified page will be shown in Excel sheet after inserting OLE object into it.

private static Image GetDocImage(string docxFile)
{
    Document document = new Document();
    document.LoadFromFile(docxFile);
    return document.SaveToImages(0, Spire.Doc.Documents.ImageType.Bitmap);

}

Step 2: Insert OLE object in Excel. After getting the worksheet from Excel file, we call GetDocImage(string doxcFile) method which is defined in the first step to get image source and then use the ws.OleObjects.Add(string FileName, Image image, OleLinkType linkType) method to insert the new OLE object to worksheet.

static void Main(string[] args)
{
    //load Excel file
    Workbook workbook = new Workbook();
    workbook.LoadFromFile("d:\\sample.xlsx");
    Worksheet ws = workbook.Worksheets[0];
    //insert OLE object
    string docx = "d:\\sample.docx";
    Image image = GetDocImage(docx);           
    IOleObject oleObject = ws.OleObjects.Add(docx,image,OleLinkType.Embed);
    oleObject.Location=ws.Range["B4"];
    oleObject.ObjectType = OleObjectType.WordDocument;
    //save the file
    workbook.SaveToFile("result.xlsx",ExcelVersion.Version2010);
    System.Diagnostics.Process.Start("result.xlsx");
}

Result:

How to Insert OLE Object in Excel in C#, VB.NET

Full Code:

[C#]
using Spire.Doc;
using Spire.Xls;
using Spire.Xls.Core;
using System.Drawing;
namespace InsertOLEObject
{
    class Program
    {
        private static Image GetDocImage(string docxFile)
        {
            Document document = new Document();
            document.LoadFromFile(docxFile);
            return document.SaveToImages(0, Spire.Doc.Documents.ImageType.Bitmap);

        }
        static void Main(string[] args)
        {
            //load Excel file
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("d:\\sample.xlsx");
            Worksheet ws = workbook.Worksheets[0];
            //insert OLE object
            string docx = "d:\\sample.docx";
            Image image = GetDocImage(docx);
            IOleObject oleObject = ws.OleObjects.Add(docx, image, OleLinkType.Embed);
            oleObject.Location = ws.Range["B4"];
            oleObject.ObjectType = OleObjectType.WordDocument;
            //save the file
            workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("result.xlsx");
        }
    }
}
[VB.NET]
Imports Spire.Doc
Imports Spire.Xls
Imports Spire.Xls.Core
Imports System.Drawing
Namespace InsertOLEObject
	Class Program
		Private Shared Function GetDocImage(docxFile As String) As Image
			Dim document As New Document()
			document.LoadFromFile(docxFile)
			Return document.SaveToImages(0, Spire.Doc.Documents.ImageType.Bitmap)

		End Function
		Private Shared Sub Main(args As String())
			'load Excel file
			Dim workbook As New Workbook()
			workbook.LoadFromFile("d:\sample.xlsx")
			Dim ws As Worksheet = workbook.Worksheets(0)
			'insert OLE object
			Dim docx As String = "d:\sample.docx"
			Dim image As Image = GetDocImage(docx)
			Dim oleObject As IOleObject = ws.OleObjects.Add(docx, image, OleLinkType.Embed)
			oleObject.Location = ws.Range("B4")
			oleObject.ObjectType = OleObjectType.WordDocument
			'save the file
			workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010)
			System.Diagnostics.Process.Start("result.xlsx")
		End Sub
	End Class
End Namespace