Word Processing: WinForms, WPF, Office File API - OLE Objects (v20.2)

Office-Inspired Products
05 February 2021

As you may already know, our Word Processing File API and our Rich Text UI controls (Word Processing for both WinForms and WPF) support OLE objects. OLE (Object Linking and Embedding) technology allows you to insert data (spreadsheets, images, presentations, charts, etc.) from external applications into your document.

With our new API, you can execute the following OLE Object-related actions:

  • create OLE objects that store links to external files or embed data from these files in your document;
  • obtain OLE object properties;
  • extract embedded content from a document;
  • remove OLE objects;
  • print and export (to PDF) documents with OLE objects.

Create OLE Objects

OLE objects are stored in the SubDocument.Shapes collection. You can use the following methods to insert OLE objects:

The following code sample creates an OLE object that stores a link to an Excel file and locks updates for this object.

Document document = wordProcessor.Document;
// Create OLE object and display it as icon.
Shape oleObject = document.Shapes.InsertOleObjectAsIcon(document.Range.Start, 
	@"Documents\ExcelWorkbook.xlsx", OleObjectType.ExcelWorksheet, 
	DocumentImageSource.FromFile(@"Images\Excel.ico"));
// Specify object position on the page.
oleObject.RelativeHorizontalPosition = ShapeRelativeHorizontalPosition.Page;
oleObject.HorizontalAlignment = ShapeHorizontalAlignment.Center;
oleObject.RelativeVerticalPosition = ShapeRelativeVerticalPosition.Margin;
oleObject.VerticalAlignment = ShapeVerticalAlignment.Top;
// Lock updates for OLE object. 
oleObject.OleFormat.AutoUpdate = false;
oleObject.OleFormat.IsLocked = true;

Please note that our Rich Text Editors (for WinForms and WPF) display OLE objects as images. You cannot modify embedded data or open files associated with OLE objects.

Retrieve Embedded Data

You can use the following methods to retrieve external data embedded in a document:

The following code sample retrieves Word and Excel data from a document and saves it to files. OleObjectType class fields are used to identify the type of OLE object content.

using DevExpress.XtraRichEdit.API.Native;
using System.IO;
// …

// Create folder to store retrieved OLE objects.
string dataDir = @"D:\Temp\OleObjects\Extracted";
if (!Directory.Exists(dataDir))
    Directory.CreateDirectory(dataDir);

// Load document with embedded OLE objects.
wordProcessor.LoadDocument(@"Documents\OleObjects.docx");

Document document = wordProcessor.Document;
// Save retrieved OLE objects to files.
for (int i = 0; i < document.Shapes.Count; i++)
{
    Shape shape = document.Shapes[i];
    if (shape.OleFormat != null)
    {
        if (shape.OleFormat.InsertType == OleInsertType.Embedded)
        {
            string extension;
            switch (shape.OleFormat.ProgId)
            {
                case OleObjectType.ExcelWorksheet:
                    extension = "xlsx";
                    break;
                case OleObjectType.Excel97_2003Worksheet:
                    extension = "xls";
                    break;
                case OleObjectType.WordDocument:
                    extension = "docx";
                    break;
                case OleObjectType.Word97_2003Document:
                    extension = "doc";
                    break;
                default:
                    extension = "bin";
                    break;
            }
            shape.OleFormat.SaveAs(Path.Combine(dataDir, 
				$"{i}.{extension}"));
         }
    }
}

In the code example below, we save retrieved Excel content to a temp file and open the resulting document in the default application associated with XLSX files.

using DevExpress.XtraRichEdit.API.Native;
using System.IO;
using System.Diagnostics;
// …

wordProcessor.LoadDocument(@"Documents\OleObjects.docx");
Document document = wordProcessor.Document;
Shape shape = document.Shapes[0];
if (shape.OleFormat != null && shape.OleFormat.InsertType == OleInsertType.Embedded 
    && shape.OleFormat.ProgId == OleObjectType.ExcelWorksheet)
{
    string path = Path.GetTempFileName().Replace(".tmp", ".xlsx");
    shape.OleFormat.SaveAs(path);
    Process.Start(path);
}

Insert OLE Objects via the User Interface

In our most recent release (v20.2.5), we implemented an Object dialog for our WinForms and WPF Rich Text Editors. This dialog allows users to create OLE objects from files. Click the Object button on the Insert ribbon tab to invoke this dialog.

ActiveX Control Support

Our DevExpress Word Processing products (File API and our Word Processors for WinForms / WPF) can now import and export ActiveX controls within a document. You can display, print, and export (to PDF) documents with ActiveX controls.

In our next major release (v21.1), we expect to introduce new APIs to manage ActiveX controls in code.

Your Feedback Matters

Should you have any questions/feedback regarding OLE Objects/ActiveX controls, please comment here or submit a support ticket via the DevExpress Support Center. We will be happy to follow-up.

FREE DevExpress Xamarin UI Controls

Deliver high-impact native mobile apps with our feature-rich Data Grid, Scheduler, Chart, TabView, Editors, and utility controls. Reserve your free copy today!
CRM-af37b6eb-e5b7-4079-b62e-e3a9ae8d8104
Customer175842
What about VBA support? It is in demand among our customers. Just don't say that there will be no support, because this is a proprietary technology. We've heard that before. About OLE and many others, you can say about the same thing, but support has appeared. We've also seen third-party implementations, too.
5 February 2021
Ben Holtzman
Ben Holtzman

I'm very happy with what you're doing with the OLE and ActiveX ability. My hope is to be able to host a spreadsheet editor inside the RichTextEditor to enable editing scenarios within the word processor. It's much less memory intensive to have a spreadsheet and chart inside an RTF file than to store images for each chart.


This is exciting news for me! Thank you for sharing!

5 February 2021

Please login or register to post comments.