Wednesday, February 24, 2010

Hosting MS Office Inside .NET Windows Forms Applications

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

FileViewer.CreateNew("Excel.Sheet")

2. PowerPoint Show Creation

FileViewer.CreateNew("PowerPoint.Show")

3. Word File Creation

FileViewer.CreateNew("Word.Document")

4. Excel Chart Creation

FileViewer.CreateNew("Excel.Chart")





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


Try






If Not xlsViewer Is Nothing Then


xlsViewer.Dispose()


xlsViewer = Nothing


End If

GC.Collect()


GC.WaitForPendingFinalizers()

Catch ex As Exception


MsgBox(ex.Message)


End Try


End Sub

9 comments:

  1. Thank you very much for your post ! Very helpful !

    ReplyDelete
  2. I appreciate you for posting such a informative post.

    ReplyDelete
  3. Seriously, thank you.

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

    Thank you!

    ReplyDelete
  4. Thanks a lot. Its really helpfull.

    ReplyDelete
  5. Hi, what is xlsviewer here?

    ReplyDelete
  6. Avaya Hosting by System delivers a new choice in Avaya cloud deployment. Resell hosted Avaya and IP Office just as you have premise, do not be turned into an agent. Avaya Cloud

    ReplyDelete
  7. This blog is truly useful to convey refreshed instructive issues over web which is truly appraisable. I discovered one effective case of this truth through this blog. I will utilize such data now. Nocciole biologiche in guscio

    ReplyDelete
  8. Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading. ms office 2016 free download full version

    ReplyDelete