Article ID: 243058 - View products that this article applies to.
This article was previously published under Q243058
Expand all | Collapse all

On This Page

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:
    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"
       ' For the 2007 Microsoft Office documents, change the .Filter parameter of the 
       ' With CommonDialog1 statement to:
       ' .Filter = "Office Documents " & _
       '      "(*.docx, *.xlsx, *.pptx)|*.docx;*.xlsx;*.pptx"
       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
    					
  5. 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.

What to consider when you use the WebBrowser control in the 2007 Microsoft Office programs

The following should be considered when you use the WebBrowser control:
  • The WebBrowser control asynchronously navigates to documents. This means that the call returns the control to your Visual Basic application before the document has been completely loaded. This occurs when you call WebBrowser1.Navigate. If you plan to automate the contained document, use the NavigateComplete2 event so that you will know when the document has finished loading. Use the Document property of the WebBrowser object to get the reference to the Office document object. In the previous code, the reference is set to oDocument.
  • Several known issues occur when you have more than one WebBrowser control in a project and each control is loaded with the same type of Office document. That is, each control contains all Word documents or all Excel spreadsheets. We recommend that you use only one control per project and that you move to one document at a time.
  • To clear the current content from the WebBrowser, use the following code to navigate to the default blank page in the Click event of another command button. Or, do this in some other appropriate place in your code.
       WebBrowser1.Navigate "about:blank"
    					

What to consider when you use the WebBrowser control in the Microsoft Office 2003 programs and in earlier versions of Office

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 about the WebBrowser control in Visual Basic, click the following article numbers to view the articles in the Microsoft Knowledge Base:
162719 How to use the WebBrowser control from Visual Basic 5.0
188271 How to print contents of the Web Browser control from VB
191692 Shdocvw.dll is not included in PDW setup package
238313 Accessing the Internet Explorer Document Object Model from Visual Basic
927009 A new window opens when you try to view a 2007 Microsoft Office program document in Windows Internet Explorer 7

Properties

Article ID: 243058 - Last Review: August 21, 2007 - Revision: 6.2
APPLIES TO
  • Microsoft Office Excel 2007
  • Microsoft Office Excel 2003
  • Microsoft Excel 2002 Standard Edition
  • Microsoft Excel 2000 Standard Edition
  • Microsoft Excel 97 Standard Edition
  • Microsoft Office PowerPoint 2007
  • Microsoft Office PowerPoint 2003
  • Microsoft PowerPoint 2002 Standard Edition
  • Microsoft PowerPoint 2000 Standard Edition
  • Microsoft PowerPoint 97 Standard Edition
  • Microsoft Office Word 2007
  • Microsoft Office Word 2003
  • Microsoft Word 2002
  • Microsoft Word 2000
  • Microsoft Word 97 Standard Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft Visual Basic 5.0 Enterprise Edition
Keywords: 
kbexpertiseinter kbhowto KB243058

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com