Articles in this section
Category / Section

How to insert different document as OLE object in the Word document.

2 mins read

Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can embed different types of document as OLE object in the Word document.

Steps to embed the multiple documents as OLE Object in Word document programmatically:

  1. Create a new C# console application project.

    Graphical user interface, application

Description automatically generated

  2. Install the Syncfusion.DocIO.Winforms NuGet package as a reference to your .NET Framework applications from NuGet.org.

    Graphical user interface, text, application, email

Description automatically generated

  3. Include the following namespace in the Program.cs file.

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using System.Drawing;

 

VB.NET

Imports Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS
Imports System.Drawing

 

  1. Use the following code example to embed the multiple documents as OLE Object in the Word document.
    //Creates a new Word document.
    WordDocument document = new WordDocument();
    //Adds new section to the document.
    IWSection section = document.AddSection();
    //Adds new paragraph to the section.
    IWParagraph paragraph = section.AddParagraph();
    //Opens the file to be embedded.
    FileStream stream = new FileStream(@"Template.pdf", FileMode.Open);
    //Loads the picture instance with the image need to be displayed.
    WPicture picture = new WPicture(document);
    picture.LoadImage(Image.FromFile(@"pdf.png"));
    //Appends PDF file as the OLE object to the paragraph.
    WOleObject oleObject = paragraph.AppendOleObject(stream, picture, OleObjectType.AdobeAcrobatDocument);
    stream.Dispose();
    oleObject.OlePicture.Height = 100;
    oleObject.OlePicture.Width = 100;
     
    //Adds new paragraph to the section.
    paragraph = section.AddParagraph();
    //Opens the file to be embedded 
    stream = new FileStream(@"Template.xlsx", FileMode.Open);
    //Loads the picture instance with the image need to be displayed.
    picture = new WPicture(document);
    picture.LoadImage(Image.FromFile(@"excel.png"));
    //Appends Excel file as the OLE object to the paragraph.
    oleObject = paragraph.AppendOleObject(stream, picture, OleObjectType.ExcelWorksheet);
    stream.Dispose();
    oleObject.OlePicture.Height = 100;
    oleObject.OlePicture.Width = 100;
     
    //Adds new paragraph to the section.
    paragraph = section.AddParagraph();
    //Opens the file to be embedded 
    stream = new FileStream(@"Adventure.docx", FileMode.Open);
    //Loads the picture instance with the image need to be displayed.
    picture = new WPicture(document);
    picture.LoadImage(Image.FromFile(@"word.png"));
    //Appends Excel file as the OLE object to the paragraph. 
    oleObject = paragraph.AppendOleObject(stream, picture, OleObjectType.WordDocument);
    stream.Dispose();
    oleObject.OlePicture.Height = 100;
    oleObject.OlePicture.Width = 100;
     
    //Adds new paragraph to the section. 
    paragraph = section.AddParagraph();
    //Opens the file to be embedded. 
    stream = new FileStream(@"Sample.pptx", FileMode.Open);
    //Loads the picture instance with the image need to be displayed. 
    picture = new WPicture(document);
    picture.LoadImage(Image.FromFile(@"powerpoint.png"));
    //Appends Excel file as the OLE object to the paragraph. 
    oleObject = paragraph.AppendOleObject(stream, picture, OleObjectType.PowerPointPresentation);
    stream.Dispose();
    oleObject.OlePicture.Height = 100;
    oleObject.OlePicture.Width = 100;
     
    //Saves the Word document. 
    document.Save("Sample.docx", FormatType.Docx);
    //Closes the document 
    document.Close();
    

 

VB.NET

'Creates a new Word document. 
Dim document As WordDocument = New WordDocument()
'Adds new section to the document. 
Dim section As IWSection = document.AddSection()
'Adds new paragraph to the section. 
Dim paragraph As IWParagraph = section.AddParagraph()
'Opens the file to be embedded. 
Dim stream As FileStream = New FileStream("../../Data/Template.pdf", FileMode.Open)
'Loads the picture instance with the image need to be displayed. 
Dim picture As WPicture = New WPicture(document)
picture.LoadImage(Image.FromFile("../../Data/pdf.png"))
'Appends PDF file as the OLE object to the paragraph. 
Dim oleObject As WOleObject = paragraph.AppendOleObject(stream, picture, OleObjectType.AdobeAcrobatDocument)
stream.Dispose()
oleObject.OlePicture.Height = 100
oleObject.OlePicture.Width = 100
 
'Adds new paragraph to the section. 
paragraph = section.AddParagraph()
'Opens the file to be embedded. 
stream = New FileStream("../../Data/Template.xlsx", FileMode.Open)
'Loads the picture instance with the image need to be displayed. 
picture = New WPicture(document)
picture.LoadImage(Image.FromFile("../../Data/excel.png"))
'Appends Excel file as the OLE object to the paragraph. 
oleObject = paragraph.AppendOleObject(stream, picture, OleObjectType.ExcelWorksheet)
stream.Dispose()
oleObject.OlePicture.Height = 100
oleObject.OlePicture.Width = 100
 
'Adds new paragraph to the section. 
paragraph = section.AddParagraph()
'Opens the file to be embedded. 
stream = New FileStream("../../Data/Adventure.docx", FileMode.Open)
'Loads the picture instance with the image need to be displayed. 
picture = New WPicture(document)
picture.LoadImage(Image.FromFile("../../Data/word.png"))
'Appends Excel file as the OLE object to the paragraph. 
oleObject = paragraph.AppendOleObject(stream, picture, OleObjectType.WordDocument)
stream.Dispose()
oleObject.OlePicture.Height = 100
oleObject.OlePicture.Width = 100
 
'Adds new paragraph to the section. 
paragraph = section.AddParagraph()
'Opens the file to be embedded. 
stream = New FileStream("../../Data/Sample.pptx", FileMode.Open)
'Loads the picture instance with the image need to be displayed. 
picture = New WPicture(document)
picture.LoadImage(Image.FromFile("../../Data/powerpoint.png"))
'Appends Excel file as the OLE object to the paragraph. 
oleObject = paragraph.AppendOleObject(stream, picture, OleObjectType.PowerPointPresentation)
stream.Dispose()
oleObject.OlePicture.Height = 100
oleObject.OlePicture.Width = 100
 
'Saves the Word document. 
document.Save("Sample.docx", FormatType.Docx)
'Closes the document. 
document.Close()

 

A complete working sample to find the list of fonts used in the Word document using C# can be downloaded from here

By executing the program, you will get the output Word document as follows.

Graphical user interface, application, Word

Description automatically generated

Take a moment to peruse the document where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion Word Framework features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion license key in your application to use the components without trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied