This article demonstrates how to navigate to an existing Office document and display it inside Visual Basic using the WebBrowser control.
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 DocumentsUse the following steps to create a Visual Basic application that opens Office documents:
- Start Visual Basic and create a new Standard project. Form1 is created by default.
- 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.
- Add an instance of the WebBrowser control, CommonDialog control, and a CommandButton to Form1.
- Next, add the following code into the Code window for Form1:
Dim oDocument As Object
Private Sub Command1_Click()
Dim sFileName As String
' Find an Office file...
.FileName = ""
sFileName = .FileName
' If the user didn't cancel, open the file...
If Len(sFileName) Then
Set oDocument = Nothing
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"
.Filter = "Office Documents " & _
"(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt"
.FilterIndex = 1
.Flags = cdlOFNFileMustExist Or cdlOFNHideReadOnly
Private Sub Form_Unload(Cancel As Integer)
Set oDocument = Nothing
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
- 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 programsThe 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.
What to consider when you use the WebBrowser control in the Microsoft Office 2003 programs and in earlier versions of OfficeThe 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:Newer versions of Internet Explorer (5.0 and greater) also allow you to display docked toolbars using the following code:
.Position = 4 '[msoBarFloating]
.Visible = True
' 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:
Article ID: 243058 - Last Review: Mar 23, 2009 - Revision: 1