How to: Use the webbrowser control to open Office Documents in Visual C # .NET (from MSDN)

xiaoxiao2021-03-06  79

For Microsoft Visual Basic .NET versions of this article, see

304643.

Overview You may want to display or embed Microsoft Office documentation directly in the Microsoft Visual C # .NET form. Visual C # .NET does not provide an OLE control for embedding an Office document in a form. If you want to embed an existing document and open it as a Visual C # .NET Form, a possible solution is to use

WebBrowser control.

This article explains how to use

The WebBrowser control browses and displays an existing Office document in the Visual C # .NET form.

More Information ActiveX documents are more embedded OLE objects that are more like ActiveX controls than traditional OLE objects. Unlike traditional embedded objects, the ActiveX document is not designed as an object contained in a larger document. Its itself can be calculated as a full document that is only available for viewing (if viewed by Microsoft Internet Explorer) or with other documents (such as Microsoft Office Recreated Files).

The ActiveX document in the WebBrowser control is always active; therefore, unlike traditional OLE embedded objects, no need to activate.

Although Microsoft Visual C # .NET does not currently carry direct load ActiveX documents, you can use

WebBrowser controls come to this purpose.

WebBrowser control (SHDOCVW.DLL) is part of Internet Explorer and can only be used on systems where Internet Explorer is installed.

Create a Visual C # .NET application that opens the Office document To create a Visual C # .NET application that open Office documents, follow these steps:

Create a Windows application project in Visual C # .NET. FORM1 is created by default. On the Tools menu, click Custom Toolbox to open the Custom Toolbox dialog. Add a reference to Microsoft WebBrowser on the COM Component tab. Click OK to add the webbrowser control to the Windows Form Toolbox. The webbrowser control will appear and with the Explorer in the toolbox. Add a webbrowser control to Form1 with this toolbox, an OpenFileDialog control, and a CommandButton button. This will add AxWebBrowser1, OpenFileDialog1, and Button1 member variables to the Form1 class. On Form1, double click Button1. This will add the Button1_Click event to Form1. In the FORM1 code window, add the following namespace to the list: use system.reflection;

As shown in the definition of a private member Form1 class: private Object oDocument; at the end of the InitializeComponent method of class Form1, add the following code to handle the Form1_Load, Form1_Closed and axWebBrowser1_NavigateComplete2 Event: this.axWebBrowser1.NavigateComplete2 = new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler (this .axwebbrowser1_navigate (this.Load = new system.eventhandler (this.form1_load);

this.closed = new system.eventhandler (this.form1_closed);

Put the following code private void button1_click (Object Sender, System.EventArgs E)

{

}

Replace 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)

}

}

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 THE

// 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.toTString ());

}

Press F5 to run the project. When you click Browse, you can open the dialog box, you can use the dialog to browse to the Word document, Excel worksheet, or PowerPoint presentation. Select any file and click Open. Documents are open in the WebBrowser control, and a message box that displays the name of Office Document Server.

Precautions when using the WebBrowser control

When the webbrowser control, you should consider the following:

The WebBrowser control is asynchronously browsed to the document. When calling webbrowser1.navigate, the call returns control to the Visual C # application before the document is fully loaded. If you intend to implement automatic operations for the included document, you must use the NaviGateComplete2 event to make a notification after the document is completed. Use the Document property of the incoming WebBrowser object to get a reference to the Office document object, in the previous code, the object is set to ODocument. The WebBrowser control does not support menu mergers. In Internet Explorer version 5.0 and later, you can use the following code to display the docking toolbar: // this is a to shodu

// Toolbars and overce to hide them. this Works with Internet Explorer 5

// But Offen Fails to Work Properly with earlier version.

Object refmissing = system.reflection.Missing.Value;

AxWebBrowser1.execwb (shdocvw.olecmdid.olecmdid_hidetoolbars, shdocvw.olecmdexecopt.olecmdexecopt_dontpromptuser, refemissing, ref refmissing;

It is known that there are multiple WebBrowser controls in one project and each control is loaded with the same type of Office document (ie, all of them are Word documents or all Excel spreadsheets). It is recommended that a project uses only one control and only views a document at a time. The most common problem occurs on the Office command bar (the command bar will be disabled). If there are two webbrowser controls on the same form, and the two controls are loaded with a Word document, and you have used one of the previous technologies to show the toolbar, then only a set of toolbars will be active, and only The toolbar can work normally. Another is disabled and cannot be used. To clear the current content in the webbrowser control, use the following code to browse to the default blank page in the Click event of another command button (or other appropriate position in the code), use the following code to browse to the default blank page: AXWebBrowser1.naviGate ("About: blank "); Refer to additional information about using the WebBrowser control, click the article number below to see the article in the Microsoft Knowledge Base:

304562 Info: Visual Studio .NET does not provide OLE container control for WINF

243058 HOWTO: Use the Webbrowser Control to Open an Office Document

162719 HOWTO: Use The WebBrowser Control from visual Basic 5.0

202476 BUG: Cannot Edit Word Document In Ole or Webbrowser Control

188271 HOWTO: Print Contents of the Web Browser Control from VB

191692 PRB: SHDOCVW.DLL IS NOT INCLUDED in PDW Setup Package

238313 PRB: Accessing The Internet Explorer Document Object Model from visual Basic

The information in this article applies to:

Microsoft Visual C # .NET (2002) Microsoft Office Excel 2003 Microsoft Excel 2002 Microsoft Excel 2000 Microsoft Office PowerPoint 2003 Microsoft PowerPoint 2002 Microsoft PowerPoint 2000 Microsoft Office Word 2003 Microsoft Word 2002 Microsoft Word 2000

Recent Updated: 2004-2-13 (6.0) Keywords: kbactivedocs Kbautomation Kbhowto KbwebBBROWSER KB304662 KBAUDDEVELOPER

转载请注明原文地址:https://www.9cbs.com/read-109025.html

New Post(0)