How To Use the WebBrowser Control to Open an Office Document in Visual Basic .NET (304643)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .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

This article was previously published under Q304643
For a Microsoft Visual C# .NET version of this article, see 304662.

SUMMARY

You may want to display, or embed, an Office document directly on a Visual Basic .NET Form. Unlike earlier versions of Visual Basic, Visual Basic .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 document and open it as an in-place ActiveX document object within a Visual Basic .NET 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 display it inside a Visual Basic .NET 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 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.

While Microsoft Visual Basic .NET does not currently support hosting ActiveX documents directly, you may 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 Basic .NET Application that Opens Office Documents

To create a Visual Basic .NET application that opens Office documents, follow these steps:
  1. Create a New Windows Application project in Visual Basic .NET. Form1 is created by default.
  2. On the Tools menu, click Customize ToolBox to open the Customize ToolBox dialog box. On the COM Components tab, add a reference to the 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.
  3. Using the toolbox, add a WebBrowser control, an OpenFileDialog control, and a button to Form1. This adds the AxWebBrowser1, OpenFileDialog1, and Button1 member variables to the Form1 class.
  4. Define a private member in the Form1 class as follows:
    Dim oDocument as Object
  5. Paste the following code in the Form1 class:
    Private Sub Button1_Click(ByVal sender As System.Object, _
       ByVal e As System.EventArgs) Handles Button1.Click
    
        Dim strFileName As String
    
        'Find the Office document.
        With OpenFileDialog1
            .FileName = ""
            .ShowDialog()
            strFileName = .FileName
        End With
    
        'If the user does not cancel, open the document.
        If strFileName.Length Then
            oDocument = Nothing
            AxWebBrowser1.Navigate(strFileName)
        End If
    
    End Sub
    
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As _
       System.EventArgs) Handles MyBase.Load
    
        Button1.Text = "Browse"
    
        With OpenFileDialog1
            .Filter = "Office Documents " & _
            "(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt"
            .FilterIndex = 1
        End With
    
    End Sub
    
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As _
       System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    
        oDocument = Nothing
    
    End Sub
    
    Private Sub AxWebBrowser1_NavigateComplete2(ByVal sender As Object, _
       ByVal e As AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event) _
       Handles AxWebBrowser1.NavigateComplete2
    
        On Error Resume Next
    
        oDocument = e.pDisp.Document
    
        'Note: You can use the reference to the document object to
        '      automate the document server.
        MsgBox("File opened by: " & oDocument.Application.Name)
    
    End Sub
    					
  6. Press F5 to run the project. When you click Browse, the Open dialog box appears and allows you to browse to a Word, Excel, or PowerPoint file. Select any file and click Open. The document opens inside the WebBrowser control, and a message box that displays the name of the Office document server appears.

Considerations When Using 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 Basic 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.
  • The WebBrowser control generally hides any docked toolbars before displaying an Office document. You can use Automation to show a floating toolbar using code such as the following:
    With oDocument.Application.CommandBars("Standard")
       .Position = 4 '[msoBarFloating]
       .Visible = True
    End With
    					
    Newer versions of Internet Explorer (5.0 and later) also allow you to display docked toolbars 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...
    AxWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_HIDETOOLBARS, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER)
    					
  • There are several known issues with having more than one WebBrowser control in a project and having each control loaded with the same type of Office document (that is, all Word documents, or all Excel spreadsheets). It is recommended that you only use one control per project, and browse to one document at a time.

    The most common problem is with 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 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 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")
    					

REFERENCES

For additional information about using the WebBrowser control, click the article numbers below to view the articles in the Microsoft Knowledge Base:

304562 INFO: Visual Studio .NET Does Not Provide an OLE Container Control for Win Forms

243058 How To Use the WebBrowser Control to Open an Office Document

162719 How To Use the WebBrowser Control from Visual Basic 5.0

202476 BUG: Cannot Edit Word Document in OLE or WebBrowser Control

188271 How To 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


Modification Type:MinorLast Reviewed:7/15/2004
Keywords:kbActiveDocs kbAutomation kbhowto kbWebBrowser KB304643 kbAudDeveloper