HOWTO: Use the WebBrowser Control to Open an Office Document

ID: Q243058


The information in this article applies to:
  • Microsoft Visual Basic Professional and Enterprise Editions for Windows, versions 5.0, 6.0
  • Microsoft Excel 2000
  • Microsoft Excel 97 for Windows
  • Microsoft PowerPoint versions 2000, 97 For Windows
  • Microsoft Word 2000
  • Microsoft Word 97 for Windows


SUMMARY

When working with Office Documents you may want to display these documents directly in Visual Basic, but do not want to create an embedded OLE object using the OLE container control. Instead, you would like to link to an existing document and open it as an in-place ActiveX Document object. Fortunately, the Microsoft WebBrowser control offers a solution.

This article demonstrates how to navigate to an existing Office document and display it inside Visual Basic using the WebBrowser control.


MORE INFORMATION

ActiveX Documents are embeddable OLE objects that behave more like ActiveX controls than traditional OLE objects. Unlike a normal embedded object, an ActiveX Document is not designed to be a contained object in a larger document. Instead, it is considered a complete document in itself, which is merely being viewed by a viewer (such as Internet Explorer) or is being collected into a single resource with other documents (such as a Binder file).

While Microsoft Visual Basic does not currently support hosting ActiveX Documents directly, you can work around this limitation by using the capabilities of Internet Explorer and its WebBrowser control. 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 Application that Opens Office Documents

Use the following steps to create a Visual Basic application that opens Office documents:
  1. Start Visual Basic and create a new Standard project. Form1 is created by default.


  2. From the Project menu, select Components to open the Components dialog box. In the Components dialog box, add references to the Microsoft Common Dialog Control and the Microsoft Internet Controls. Click OK to add the items to the toolbox.


  3. Add an instance of the WebBrowser control, CommonDialog control, and a CommandButton to Form1.


  4. Next, add the following code into the Code window for Form1:


  5. 
    Option Explicit
    
    Dim oDocument As Object
    
    Private Sub Command1_Click()
       Dim sFileName As String
       
     ' Find an Office file...
       With CommonDialog1
          .FileName = ""
          .ShowOpen
          sFileName = .FileName
       End With
       
     ' If the user didn't cancel, open the file...
       If Len(sFileName) Then
          Set oDocument = Nothing
          WebBrowser1.Navigate sFileName
       End If
    End Sub
    
    Private Sub Form_Load()
       Command1.Caption = "Browse"
       With CommonDialog1
          .Filter = "Office Documents " & _
          "(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt"
          .FilterIndex = 1
          .Flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly
       End With
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
       Set oDocument = Nothing
    End Sub
    
    Private Sub WebBrowser1_NavigateComplete2(ByVal pDisp As Object, _
    URL As Variant)
       On Error Resume Next
       Set oDocument = pDisp.Document
       MsgBox "File opened by: " & oDocument.Application.Name
    End Sub 
  6. Press F5 to run the project. When you select the Browse button, the Open dialog box appears allowing you to navigate to a Word, Excel or PowerPoint file. Choose Open and the document should open inside the WebBrowser control. A message box then appears that displays the name of the Office application that opened the file.


Considerations When Using the WebBrowser Control

The following should be considered when you use the WebBrowser control:
  • The WebBrowser control navigates to documents asynchronously. This means that 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 know when the document has finished loading. Use the Document property of the WebBrowser object 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. If you need the document's menu items to appear with your Visual Basic menu, you must use the OLE container control instead.


  • 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 this:


  • 
       With oDocument.Application.CommandBars("Standard")
          .Position = 4 '[msoBarFloating]
          .Visible = True
       End With 
    Newer versions of Internet Explorer (5.0 and greater) 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...
       WebBrowser1.ExecWB OLECMDID_HIDETOOLBARS, 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 navigate 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 loaded with Word documents, and you have displayed toolbars using one of the preceding techniques, only one set of toolbars will be active and work correctly. The other will be 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), navigate to the default blank page using this code:


  • 
       WebBrowser1.Navigate "about:blank" 


REFERENCES

For more information on the WebBrowser control in Visual Basic, please see the following articles in the Microsoft Knowledge Base:

Q162719 HOWTO: Use the WebBrowser Control from Visual Basic 5.0

Q188271 HOWTO: Print Contents of the Web Browser Control From VB

Q191692 PRB: SHDOCVW.DLL Not Included in PDW Setup Package

Q238313 PRB: Accessing the Internet Explorer Document Object Model w/ VB

© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Richard R. Taylor, Microsoft Corporation

Additional query words: web browser kbActiveDocs kbExcel kbVBp500 kbVBp600 kbWebBrowser kbPowerPt kbWord

Keywords : kbGrpDSO kbDSupport
Version : WINDOWS:2000,5.0,6.0,97
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: November 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.