打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

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

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() ); }					
    NoteYou 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://support.microsoft.com/kb/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://support.microsoft.com/kb/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://support.microsoft.com/kb/304562/ )
Visual Studio 2005 and Visual Studio .NET do not provide an OLE container control for Windows Forms
243058
(http://support.microsoft.com/kb/243058/ )
How to use the WebBrowser control to open an Office document
162719
(http://support.microsoft.com/kb/162719/ )
How to use the WebBrowser control from Visual Basic 5.0
202476
(http://support.microsoft.com/kb/202476/ )
BUG: Cannot edit Word document in OLE or WebBrowser control
188271
(http://support.microsoft.com/kb/188271/ )
How to print contents of the Web Browser control from VB
191692
(http://support.microsoft.com/kb/191692/ )
Shdocvw.dll is not included in PDW setup package
238313
(http://support.microsoft.com/kb/238313/ )
Accessing the Internet Explorer Document Object Model from Visual Basic
927009
(http://support.microsoft.com/kb/927009/ )
A new window opens when you try to view a 2007 Microsoft Office program document in Windows Internet Explorer 7

Properties

Article ID: 304662 - Last Review: September 14, 2007 - Revision: 9.1
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
  • Microsoft Word 2000
Keywords: 
kbactivedocs kbautomation kbhowto kbwebbrowser KB304662
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
扫描仪与AXMODI组件
C#包含mshtml, SHDocVw, AxSHDocVw, IHTMLDocument2 的引用
利用word将PDF转换DOC文件的方法 --电脑高手
适用于 Visual Basic 6.0 用户的 WebBrowser 控件
How To Cause Navigation to Occur in Same WebB...
没有扫描仪,怎么办?用什么代替?
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服