Hosting MS Office Inside .NET Windows Forms Applications

Posted by VIJI Wednesday, February 24, 2010

The purpose of this article is to discuss how to build Office solutions in Visual Studio .NET.

WebBrowser Control – (available from the Visual Studio .NET WinForms designer toolbox) which allows simply to navigate to a file-URL pointing to an office document.

The problem is, WebBrowser is nothing else than the well known Internet Explorer webbrowser control, so its behavior depends on the client's internet settings.

E.g. when "Confirm open after download" has been set, a file download dialog will appear when programmatically opening an Office document (even in case of a local file), and after confirming the document shows up in a new top-level Word or Excel window, not inside the webbrowser control's host (= your application).

That's why I switched from WebBrowser to the DSOFramer ActiveX control provided by Microsoft, which doesn't have these restrictions.

DSOFramer Control

The DsoFramer control is an ActiveX control provided by Microsoft that allows you to host instances of Office applications (Excel, Word , Visio, Power Point etc) inside our ActiveX-capable application, including Windows Forms. The DSO Framer control is compatible with Office 2007, so we can embed all office 2007 applications.

DSOFramer Control in Windows Forms

Steps to use DSOFramer Control in Windows Forms application

1. Download the DSOFramer Control from Microsoft Website and install it in your machine.

2. Open a new Visual Studio Project

3. Add New Form

4. Right Click on Tool Box -> Click Choose Items …

5. Select Com Components Tab

6. Select DSO Framer Control Object

7. Click On Ok.

8. Drag and Drop the Control DSO Framer onto the Form

9. Name the control as AxFramerControl1

The following VB.NET Code examples explains how to create a new office document(Excel, Word etc), open existing office document (Excel, Word etc).

Opening Office Documents

Opening an Excel File

Dim FileViewer As AxDSOFramer.AxFramerControl = Nothing

Dim _FileName As String = ""

_FileName = "C:\Test.xls"

FileViewer = Me.AxFramerControl1

FileViewer.Open(_FileName, True, Type.Missing, Type.Missing, Type.Missing)

' To open file from Web site With UserId and password

FileViewer. Open ("https://Myserver/test/mytest.asp?id=XFile", True, " Excel.Sheet ", "MyUserAccount", "MyPassword")

The File Extensions supported are .xls, .xlsx, .doc, .docx, .ppt and .pptx .

To Open word document,

_FileName = "C:\Test.doc"

_FileName = "C:\Test.docx" ' Office 2007

To Open Power Point,

_FileName = "C:\Test.ppt"

_FileName = "C:\Test.pptx" ' Office 2007

Create a New instance of Office application

To create an instance of an office application, the ProgId needs to be passed to the CreateNew function. Some of the examples are given below:

1. Excel Sheet Creation


2. PowerPoint Show Creation


3. Word File Creation


4. Excel Chart Creation


Saving Files

We can save the application by passing the FileName. For Example,

FileViewer.Save "C:\aa.doc" , will save the file with the name aa.doc.

Customization of DSOFramer

The DSOFramer can be customized to hide/show the toolbar, menu bar and disable/enable the menu options.

AxFramerControl1.Toolbars = False 'Hide Tool Bar

AxFramerControl1.Menubar = False 'Hide Menu Bar

AxFramerControl1.set_EnableFileCommand(DSOFramer.dsoFileCommandType.dsoFileNew, False) 'Disable the File New Option

AxFramerControl1.set_EnableFileCommand(DSOFramer.dsoFileCommandType.dsoFileOpen, False) 'Disable the File Open Option

AxFramerControl1.set_EnableFileCommand(DSOFramer.dsoFileCommandType.dsoFileClose, False) 'Disable the File Close Option

AxFramerControl1.set_EnableFileCommand(DSOFramer.dsoFileCommandType.dsoFileSave, False) 'Disable the File Save Option

AxFramerControl1.set_EnableFileCommand(DSOFramer.dsoFileCommandType.dsoFileProperties, False) 'Disable the File Properties Option

AxFramerControl1.set_EnableFileCommand(DSOFramer.dsoFileCommandType.dsoFilePrintPreview, False) 'Disable the File Print Preview Option

Destroying the instance

Whenever we create an instance of an office application, we must destroy it explicitly as it will not get destroyed automatically.

If we don’t destroy the instance, for example after opening the file ‘c:\aa.doc’ in the DSOFramer , close the form and tries to open the file again it will throw the error “Cannot access the file”

The following Code snippet explains how to destroy the instance and do explicit garbage collection.

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing


If Not xlsViewer Is Nothing Then


xlsViewer = Nothing

End If



Catch ex As Exception


End Try

End Sub

5 Responses to Hosting MS Office Inside .NET Windows Forms Applications

  1. Ludovic Says:
  2. Thank you very much for your post ! Very helpful !

  3. I appreciate you for posting such a informative post.

  4. Anonymous Says:
  5. Seriously, thank you.

    It is very hard to find any documentation on the DSOFramer control.

    Thank you!

  6. nice blog

  7. Anonymous Says:
  8. Thanks a lot. Its really helpfull.


Post a Comment