OLE Object

This example demonstrates how to insert an OLE object to a document.

OLE is a compound document standard. It enables us to create objects with one application and then link or embed them in a another one.

Attachments also store data more efficiently. Object Linking and Embedding (OLE) is used to store images and documents. OLE created a bitmap equivalent of the image or document.

We have versions of this representation in words ,using dropdown you can make a choice within the following

  • Word Doc
  • Word Docx
  • Word ML
  • RTF
  • MS Text format
  • E-book format
  • DLS based xml file format
  • HTML

After clicking a generate button will launch a file with above choosen version of word with Chart OLE Object from the source file.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;

using OfficeComponent.Word;


namespace OfficeComponent.Samples
{
    class OLEObjectExample : WordExampleBase
    {
        public OLEObjectExample(string commonDataPath, string outputDir)
            : base(commonDataPath, outputDir)
        {

        }

        public OLEObjectExample(string commonDataPath, string outputDir, string xmlFile) : base(commonDataPath, outputDir, xmlFile)
        {

        }

        public override string Execute()
        {
            // Create a new instance of PdfDocument class.
            WordDocument document = new WordDocument();

            document.EnsureMinimum();

            // Open a document that contains OLE objects.
            WordDocument oleDocument = new WordDocument(CommonDataPath + "\\OLE Objects.docx");

            // Get an OLE object from the source document.
            WordOleObject oleObject = oleDocument.LastParagraph.Items[0] as WordOleObject;

            // Add a new paragraph to the new document.
            Paragraph para = document.Sections[0].AddParagraph();
            para.AppendText("Chart OLE Object from the source file");
            para.ApplyStyle(StyleIdentifier.Heading2);

            para = document.Sections[0].AddParagraph();
            // Append the OLE object to the new document as an embedded object.
            para.AppendOleObject(oleObject.Container, (Picture)oleObject.OlePicture.Clone(), OleLinkType.Embed);

            string fileName = Path.Combine(OutputDir, this.GetType().Name + "_" + Guid.NewGuid().ToString() + GetExtension(SaveAsFormat));
            // Save the document.
            document.Save(fileName, SaveAsFormat);

            // Close the document.
            document.Close();

            return fileName;
        }
        

    }

}
Imports System.Drawing.Imaging
Imports System.IO
Imports System.Text

Imports OfficeComponent.Word


Namespace OfficeComponent.Samples
	Friend Class OLEObjectExample
		Inherits WordExampleBase
		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String)
			MyBase.New(commonDataPath, outputDir)

		End Sub

		Public Sub New(ByVal commonDataPath As String, ByVal outputDir As String, ByVal xmlFile As String)
			MyBase.New(commonDataPath, outputDir, xmlFile)

		End Sub

		Public Overrides Function Execute() As String
			' Create a new instance of PdfDocument class.
			Dim document As New WordDocument()

			document.EnsureMinimum()

			' Open a document that contains OLE objects.
			Dim oleDocument As New WordDocument(CommonDataPath & "\OLE Objects.docx")

			' Get an OLE object from the source document.
			Dim oleObject As WordOleObject = TryCast(oleDocument.LastParagraph.Items(0), WordOleObject)

			' Add a new paragraph to the new document.
			Dim para As Paragraph = document.Sections(0).AddParagraph()
			para.AppendText("Chart OLE Object from the source file")
			para.ApplyStyle(StyleIdentifier.Heading2)

			para = document.Sections(0).AddParagraph()
			' Append the OLE object to the new document as an embedded object.
			para.AppendOleObject(oleObject.Container, CType(oleObject.OlePicture.Clone(), Picture), OleLinkType.Embed)

			Dim fileName As String = Path.Combine(OutputDir, Me.GetType().Name & "_" & Guid.NewGuid().ToString() & GetExtension(SaveAsFormat))
			' Save the document.
			document.Save(fileName, SaveAsFormat)

			' Close the document.
			document.Close()

			Return fileName
		End Function


	End Class

End Namespace