Microsoft Knowledge Base Email Alertz

KBAlertz.com: (304662) - You may want to display or to embed a Microsoft Office document directly on a Microsoft Visual C# .NET form. Visual C# .NET does not provide an OLE control that allows you to embed an Office document on a form. If you want to embed an existing...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 304662 - Last Review: September 14, 2007 - Revision: 9.0

How to use the WebBrowser control to open Office documents in Visual C# 2005 or in Visual C# .NET

This article was previously published under Q304662
For a Microsoft Visual Basic .NET version of this article, see 304643  (http://kbalertz.com/Feedback.aspx?kbNumber=304643/ ) .

On This Page

SUMMARY

You may want to display or to embed a Microsoft Office document directly on a Microsoft Visual C# form. Microsoft Visual C# 2005 and Microsoft Visual C# .NET do not provide an OLE control that allows you to embed an Office document on a form. If you want to embed an existing document and open it as an in-place ActiveX document object in a Visual C# form, a potential solution for you is to use the Microsoft WebBrowser control.

This article demonstrates how to browse to an existing Office document and how to display it inside a Visual C# form by using the WebBrowser control.

MORE INFORMATION

ActiveX documents are embeddable OLE objects that behave more like ActiveX controls than traditional OLE objects. Unlike a traditional embedded object, an ActiveX document is not designed to be a contained object in a larger document. Instead, it is considered in itself a complete document that is merely being viewed (such as with Microsoft Internet Explorer) or being collected into a single resource with other documents (such as a Microsoft Office Binder file). An ActiveX document that is hosted in the WebBrowser control is always active; therefore, unlike traditional OLE embedded objects, there is no sense of in-place activation.

Although Visual C# 2005 and Visual C# .NET do not currently support hosting ActiveX documents directly, you can use the WebBrowser control for this purpose. The WebBrowser control (Shdocvw.dll) is a part of Internet Explorer and can only be used on systems that have Internet Explorer installed.

Creating a Visual C# application that opens Office documents

To create a Visual C# application that opens Office documents, follow these steps:
  1. Create a new Windows Application project in Visual C# 2005 or in Visual C# .NET. Form1 is created by default.

    Note In Visual C# 2005, if you cannot find the SHDocVw.dll file or the AxSHDocVw.dll file, run the following command at the Visual Studio command prompt:
    aximp %WINDIR%\system32\shdocvw.dll
    Then, create a common language runtime proxy (SHDocVw.dll) and a Windows Forms proxy (AxSHDocVw.dll) for the Microsoft Web Browser control. To add the DLL files in Visual C# 2005, follow these steps:
    1. On the Project menu, click Add Reference.
    2. In the Add Reference dialog box, click Browse.
    3. Locate and then select the AxSHDocVw.dll file and the SHDocVw.dll file.
    4. To add project references for these two files, click OK.
  2. On the Tools menu, click Customize ToolBox to open the Customize ToolBox dialog box. On the COM Components tab, add a reference to Microsoft WebBrowser. Click OK to add the WebBrowser control to the Windows Forms toolbox. The WebBrowser control appears with the text Explorer in the toolbox.

    Note In Visual Studio 2005, you do not have to do step 2.
  3. Using the toolbox, add a WebBrowser control, an OpenFileDialog control, and a CommandButton control to Form1. This adds the AxWebBrowser1, OpenFileDialog1, and Button1 member variables to the Form1 class. In Visual C# 2005, the webBrowser1, openFileDialog1, and button1 member variables are added.
  4. On Form1, double-click Button1. This adds the Button1_Click event to Form1.
  5. In the code window for Form1, add the following namespace to the list:
    using System.Reflection;
    					
  6. Define a private member in class Form1 as follows:
    private Object oDocument; 
  7. At the end of the InitializeComponent method of class Form1, add the following code to handle the Form1_Load, Form1_Closed, and axWebBrowser1_NavigateComplete2 events:
    this.axWebBrowser1.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.axWebBrowser1_NavigateComplete2);
    this.Load += new System.EventHandler(this.Form1_Load);
    this.Closed += new System.EventHandler(this.Form1_Closed);
    					
  8. Replace the following code
    private void button1_Click(object sender, System.EventArgs e)
    {
    }
    					
    with:
    private void button1_Click(object sender, System.EventArgs e)
    {
    
     String  strFileName;
    
     //Find the Office document.
     openFileDialog1.FileName = "";
     openFileDialog1.ShowDialog();
     strFileName = openFileDialog1.FileName;
    	        
     //If the user does not cancel, open the document.
     if(strFileName.Length != 0)
     {
      Object refmissing = System.Reflection.Missing.Value;
      oDocument = null;
      axWebBrowser1.Navigate(strFileName, ref refmissing , ref refmissing , ref refmissing , ref refmissing);
     }
    }
    
    public void Form1_Load(object sender, System.EventArgs e)
    {
     button1.Text = "Browse";
     openFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt" ;
     openFileDialog1.FilterIndex = 1;
    }
    
    public void Form1_Closed(object sender, System.EventArgs e)
    {
     oDocument = null;
    }
    
    public void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event e)
    {
    
     //Note: You can use the reference to the document object to 
     //      automate the document server.
    
     Object o = e.pDisp;
       
     oDocument = o.GetType().InvokeMember("Document",BindingFlags.GetProperty,null,o,null);
    			
     Object oApplication = o.GetType().InvokeMember("Application",BindingFlags.GetProperty,null,oDocument,null);
    
     Object oName = o.GetType().InvokeMember("Name",BindingFlags.GetProperty ,null,oApplication,null);
    
     MessageBox.Show("File opened by: " + oName.ToString() ); 
    }
    					
    Note You must change the code in Visual Studio 2005. By default, Visual C# adds one form to the project when you create a Windows Forms project. The form is named Form1. The two files that represent the form are named Form1.cs and Form1.designer.cs. You write the code in Form1.cs. The Form1.designer.cs file is where the Windows Forms Designer writes the code that implements all the actions that you performed by dragging and dropping controls from the Toolbox.

    For more information about the Windows Forms Designer in Visual C# 2005, visit the following Microsoft Developer Network (MSDN) Web site:
    http://msdn2.microsoft.com/en-us/library/ms173077.aspx (http://msdn2.microsoft.com/en-us/library/ms173077.aspx)
  9. Press F5 to run the project. When you click Browse, the Open dialog box appears and allows you to browse to a Word document, an Excel worksheet, or a PowerPoint presentation. Select any file, and then click Open. The document opens inside the WebBrowser control, and a message box that displays the name of the Office document server appears.

What to consider when you use the WebBrowser control

You should consider the following when you use the WebBrowser control:
  • The WebBrowser control browses to documents asynchronously. When you call WebBrowser1.Navigate, the call returns control to your Visual C# application before the document has been completely loaded. If you plan on automating the contained document, you need to use the NavigateComplete2 event to be notified when the document has finished loading. Use the Document property of the WebBrowser object that is passed in to get a reference to the Office document object, which, in the preceding code, is set to oDocument.
  • The WebBrowser control does not support menu merging.
  • In Internet Explorer versions 5.0 and later, you can display docked toolbars by using the following code:
     // This is a toggle option, so call it once to show the 
     // toolbars and once to hide them. This works with Internet Explorer 5
     // but often fails to work properly with earlier versions.
     
     Object refmissing = System.Reflection.Missing.Value;
     axWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER,ref refmissing , ref refmissing);
    					
  • There are several known issues with having more than one WebBrowser control in a project and with having each control loaded with the same type of Office document (that is, all Word documents or all Excel worksheets). Microsoft recommends that you use only one control for each project, and browse to one document at a time.

    The most common problem is with Microsoft Office command bars, which appear disabled. If you have two WebBrowser controls on the same form, both of which are loaded with Word documents, and if you have displayed toolbars by using one of the preceding techniques, only one set of toolbars is active and works correctly. The other is disabled and cannot be used.
  • To clear the WebBrowser control of its current contents, in the Click event of another command button (or in some other appropriate place in your code), browse to the default blank page by using the following code:
       AxWebBrowser1.Navigate("about:blank");
    					

What to consider when you use the WebBrowser control together with a 2007 Microsoft Office program

By default, the 2007 Office programs do not open Office documents in the Web browser. This behavior also affects the WebBrowser control. We recommended that you use a custom ActiveX document container instead of the WebBrowser control when you develop applications that open 2007 Office documents. For more information about custom ActiveX document containers, click the following article number to view the article in the Microsoft Knowledge Base:
311765  (http://kbalertz.com/Feedback.aspx?kbNumber=311765/ ) Visual C++ ActiveX control for hosting Office documents in Visual Basic or HTML
For existing applications that require backward compatibility with the WebBrowser control, you can modify the registry to configure Internet Explorer. You can use this method to configure Internet Explorer to open 2007 Office documents in the Web browser. For more information, click the following article number to view the article in the Microsoft Knowledge Base:
927009  (http://kbalertz.com/Feedback.aspx?kbNumber=927009/ ) A new window opens when you try to view a 2007 Microsoft Office program document in Windows Internet Explorer 7
Note If you modify the registry by using the method that is mentioned in Microsoft Knowledge Base article 927009, the changes affect the WebBrowser control that you use in the application. The changes also affect all instances of Internet Explorer. Additionally, this method may not work for any future versions of the Microsoft Office suites. Therefore, we recommend that you use this method only for compatibility with an existing application.

REFERENCES

For more information about using the WebBrowser control, click the following article numbers to view the articles in the Microsoft Knowledge Base:
304562  (http://kbalertz.com/Feedback.aspx?kbNumber=304562/ ) Visual Studio 2005 and Visual Studio .NET do not provide an OLE container control for Windows Forms
243058  (http://kbalertz.com/Feedback.aspx?kbNumber=243058/ ) How to use the WebBrowser control to open an Office document
162719  (http://kbalertz.com/Feedback.aspx?kbNumber=162719/ ) How to use the WebBrowser control from Visual Basic 5.0
202476  (http://kbalertz.com/Feedback.aspx?kbNumber=202476/ ) BUG: Cannot edit Word document in OLE or WebBrowser control
188271  (http://kbalertz.com/Feedback.aspx?kbNumber=188271/ ) How to print contents of the Web Browser control from VB
191692  (http://kbalertz.com/Feedback.aspx?kbNumber=191692/ ) Shdocvw.dll is not included in PDW setup package
238313  (http://kbalertz.com/Feedback.aspx?kbNumber=238313/ ) Accessing the Internet Explorer Document Object Model from Visual Basic
927009  (http://kbalertz.com/Feedback.aspx?kbNumber=927009/ ) A new window opens when you try to view a 2007 Microsoft Office program document in Windows Internet Explorer 7

APPLIES TO
  • Microsoft Visual C# 2005
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Office Excel 2007
  • Microsoft Office Excel 2003
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Office PowerPoint 2007
  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft PowerPoint 2000 Standard Edition
  • Microsoft Office Word 2007
  • Microsoft Office Word 2003
  • Microsoft Word 2002 Standard Edition
  • Microsoft Word 2000 Standard Edition
Keywords: 
kbhowto kbactivedocs kbwebbrowser kbautomation KB304662
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please