Embedding MS Word, Excel, PowerPoint into a WPF Application

> Edraw Tip > Embedding MS Word, Excel, PowerPoint into a WPF Application
author
Posted by James Freeman |
Edraw office viewer component is the simplest and reliable solution allows the developers to host MS Word documents, Excel worksheets, PowerPoint presentations into a WPF application

How to embed MS Word, Excel, PowerPoint into a WPF application? I suppose most of you remember the good old OLE technology which allowed embedding Excel diagrams into Word documents etc. But the technology doesn't support all the Microsoft office document. It doesn't support the multiple MS Word instance in a form. Edraw office viewer component, as the alternatives on the internet, is the simplest and most reliable solution allows the developers to host MS Word documents, Excel worksheets, PowerPoint presentations into a WPF application.

Using this WPF component the developers can embed MS Word, MS Excel, MS PowerPoint into WPF application by placing an instance of Office Viewer onto the main form.

Click Here to Download Office Component - Support MS Word, Excel, PowerPoint, Visio, Project for WPF Program.

free download5 MB

The following article will demo how to embed a MS word in a wpf application step by step.

If you haven't the officeviewer.ocx file, you need TO install the package firstly. In the component install folder, you can also find the wpf sample project.

Open the Visual Studio and create a new WPF application.

Right Click the WpfApplication1 Solution. Then click Add menu and point the User Control...

add user control

A New window form will be added in the wpf project.

Choose the "User Control" item. Not the "User Control (WPF)" item.

Double click the UserControl1.CS in the Solution panel.

Open the Toolbox panel, then click the Choose Items... in the context menu.

choose items

In the pop up Choose Toolbox Items dialog, select the Edraw Office Viewer Component then click the Ok.

add office viewer component

Now the Edraw Office Viewer Component was added in the General tab in the Toolbox. Drag it in the UserControl form.

add office references

The AxEDofficeLib and EDOfficeLib will be added into the solution by the Visual Studio wizard.

Type the following C# codes for opening a word document and protect the word document from modification looked like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WpfApplication1
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public void Open()
{
axEDOffice1.OpenFileDialog();
}
public void Protect()
{
if (axEDOffice1.GetCurrentProgID() == "Word.Application")
{
axEDOffice1.ProtectDoc(2);
}
}
public void Print()
{
axEDOffice1.PrintPreview();
}
public void Close()
{
axEDOffice1.ExitOfficeApp();
}
}
}

At last, you need to write a host window for the UserControl. Switch to the Windows1.xaml file then add the open, protect, print and close button as the following image.

add wpf form

Add the following c# code to associate the office component.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Open_Click(object sender, RoutedEventArgs e)
{
_host.Open();
}
private void Protect_Click(object sender, RoutedEventArgs e)
{
_host.Protect();
}
private void Print_Click(object sender, RoutedEventArgs e)
{
_host.Print();
}
private void Close_Click(object sender, RoutedEventArgs e)
{
_host.Close();
}
}
}

Open the Configuration Manager. Change the Active solution platform as x86 option. Then build and run.

WPF Embed Word

The office viewer component support all versions of MS Word. To embed MS Excel or PowerPoint, Visio, Project into a WPF application, you needn't change anything, only call the Open method as follow:

public void Open()
{
//axEDOffice1.OpenFileDialog();
axEDOffice1.Open(sPath, "Word.Application");
axEDOffice1.Open(sPath, "Excel.Application");
axEDOffice1.Open(sPath, "PowerPoint.Application");
axEDOffice1.Open(sPath, "Visio.Application");
axEDOffice1.Open(sPath, "MSProject.Application");
}

Embedding MS Office in ASP.NET Program

Embedding MS Project

Embedding Visio

An Easy Way to Embed Excel in a Web Page

Embedded MS PowerPoint

Disables MS Word Standard Command

Disable Office Ribbon Button

Show/Hide Office Menu Bar

download EdrawMind
main page

Get Started! You Will Love This Easy-To-Use Diagram Software

EdrawMax is an advanced all-in-one diagramming tool for creating professional flowcharts, org charts, mind maps, network diagrams, UML diagrams, floor plans, electrical diagrams, science illustrations, and more. Just try it, you will love it!