OLE with .NET - Part 2 - Word

This topic will give you an overview of the concepts, as well as the use cases related to OLE in Excel with the .NET platform.

What is OLE?

  • OLE, short for Object Linking & Embedding, is a proprietary technology developed by Microsoft that allows embedding and hyperlinks with documents and other objects. For developers, it offers OLE Control Extension (OCX), a way to develop and use custom user interface elements. At the technical level, an OLE object is any object that implements the IOleObject interface, possibly along with a range of other interfaces, depending on the needs of the object.

Overview

  • OLE allows an editing application to export a part of a document to another editing application and then import it with additional content. For example, a desktop publishing system can send some text to a word processor or image to a bitmap editor using OLE. The main benefit of OLE is the ability to add different types of data to a document from different applications, such as text editors and image editors. This feature creates a compound binary file format document and a master file for which the document makes references. Changing the data in the main file immediately affects the reference to it. This is called "linking" (instead of "embedding").
  • Its primary use is for managing compound binary file formats, and transferring data between different application software using drag and drop and clipboard.

Specifications

  • Component Object Model includes these OLE objects and containers; they are objects that can implement interfaces to export their functions. Only the IOleObject interface is required, but we may also need to perform other interfaces if the functionality that is exported by those interfaces is needed. To better understand what is said below, some terms need to be clarified. The view state of the object is whether the object is transparent, opaque, or opaque to a homogeneous background and whether the object supports drawing with a specified aspect. The page of an object is an object that represents the position of the object in its container. A container supports the storage of objects for all objects. The following is a list of interfaces, grouped by the object they usually need to implement. Interfaces often implemented by OLE objects are often called by OLE containers and vice versa. Note that the following indented list indicates the inheritance of the interface. IUnknown provide all of the non-indented interfaces.

OLE object

DataObject
  • When deployed, it allows data transfer and data change notification.
ObjectWithSite
  • Allow callers to notify OLE objects of its website. OOOject also provide this functionality, we can use ObjectWithSite when supported, if we do not use OleObject for other things.
OleCache
  • Allow visual display from stored DataObject. This DataObject allows embedded objects to store their visual representation, therefore allowing it to be displayed later without having to launch the application that used to create the object.
  • Usually, the implementation will retrieve the content stored in the object.
  • OleCache2
  • Provides more precise control in caching.
  • Usually, the implementation will retrieve the content stored in the object.
OleCacheControl
  • We won't call this interface by the container but will call by the internal of the object that allows it to receive notifications when the DataObject is running. Thus, this will enable it to register notifications of that object's data change and therefore allow it to update the cached presentation properly.
  • Usually, the implementation will retrieve the content stored in the object.
OleDocument
  • Enables the OLE object to support its multiple data views, as well as several related functions.
OleDocumentView
  • A document object (an OleDocument implementation object) implements this interface for each request to view the object. It allows callers to set the page of the object, query and set the size of the object and to display and activate it, as well as several related functions.
OleWindow
  • OleInPlaceActiveObject
  • Called by the outermost container of an object to interact with it while it is active, for example, to handle acceleration keys in the message queue of the container meant for the container object.
  • OleInPlaceObject
  • Called by the container to activate or deactivate the object.
  • A windowless object is an object without its window. It is used by the container to forward messages received by the container window designed for the container object. For example, if we move the cursor over a window screen, Windows places a mouse movement notification along with the mouse coordinates in the window's message queue. If this window contains any windowless embedded object, the message may have to be forwarded to such an object if the mouse pointer coordinates are on the object. For the same reason, this interface also provides access to the object's DropTarget interface.
OleLink
  • Allow the object to support binding, for example, by allowing the container to place the source of the associated object. Usually, the deployment will get the deployment content cached in the object.
OleObject
  • Arguably this is the most crucial interface for an OLE object. For example, it allows the container to notify its object, initialize the object from the data, open and close it, to query or set the object's size, request notification on the container's AdviseSink and Show objects that have been defined as verbs on the object. These verbs often include Open or Edit, but can also add other verbs. One of the verbs is defined as the main verb and will perform it when the user double clicks on an object.
ViewObject

Lets an object draw itself directly, without moving a DataObject into the container. For those objects that support both DataObject and this interface, we often share the following deployment commands.

  • ViewObject2
  • Allows callers to query the size of the object.
  • ViewObjectEx
  • Add support for flicker-free display of transparent objects, experiment with irregularly shaped objects, and set the size of an object.

Microsoft Word with OLE in .NET

Recognizing the outstanding features and necessity of OLE, we are pleased to present the Word Office Component library - an all-powerful library that allows you to perform various complex tasks with Word documents with OLE on the .NET platform.

Note: This example applies to C# platforms. This example will demonstrate how to extract an OLE Object from an existing Excel document that stored on your computer. We have fully installed all of the methods used in this article in the Excel Office Component library, so make sure you have our library installed. First of all, import the libraries needed for this task.

  • C# Version: using OfficeComponent.Word;
  • VB Version: Import OfficeComponent.Word

First and foremost, you'll want to create your own Word document for the next steps. We can quickly archive this by taking the advance of the WordDocument class - which is already fully installed in the Word Office Component library.

  • C# Version: WordDocument document = new WordDocument();
  • VB Version: Dim document As New WordDocument()

To open a Word document, you can also take advantage of the WordDocument class as the code snippet below.

  • C# Version: WordDocument oleDocument = new WordDocument(CommonDataPath + "\\OLE Objects.docx");
  • VB Version: Dim oleDocument As New WordDocument(CommonDataPath & "\OLE Objects.docx")

The WordOleObject class allows you to retrieve OLE Objects from the source document.

  • C# Version: WordOleObject oleObject = oleDocument.LastParagraph.Items[0] as WordOleObject;
  • VB Version: Dim oleObject As WordOleObject = TryCast(oleDocument.LastParagraph.Items(0), WordOleObject)

The Paragraph class will represent the paragraphs in the document. It could be a piece of text selected from existing documents or a new piece of writing to be added to the document.

  • C# Version: Paragraph para = document.Sections[0].AddParagraph();
  • VB Version: Dim para As Paragraph = document.Sections(0).AddParagraph()

To add new content to an existing paragraph, you can take advantage of the AppendText method:

  • C# Version: para.AppendText("Chart OLE Object from the source file");
  • VB Version: para.AppendText("Chart OLE Object from the source file")

If the content that you want to add to your document is an OLE object, the AppendOleObject method is just for you.

  • C# Version: para.AppendOleObject(oleObject.Container, (Picture)oleObject.OlePicture.Clone(), OleLinkType.Embed);
  • VB Version: para.AppendOleObject(oleObject.Container, CType(oleObject.OlePicture.Clone(), Picture), OleLinkType.Embed)

The full source code of these examples is also available in our Word package.

A live demo for OLE Object is also available on our site. If you also need Word functionality, check out our Word online demos.

45-Day Money Back Guarantee

We will refund your full money in 45 days
if you are not satisfied with our products

Buy Now
You have successfully subcribed to our mailing list.
Dont miss out Get update on new articles and other opportunities Subscribe