This article demonstrates how to browse to an existing Office document and display it in a Visual Basic form by using the WebBrowser control.
While Microsoft Visual Basic .NET and Visual Basic 2005 do 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 application that opens Office documentsTo create a Visual Basic application that opens Office documents, follow these steps:
- In Microsoft Visual Studio 2005 or in Microsoft Visual Studio .NET, create a Windows Application project by using Visual Basic 2005 or Visual Basic .NET. Form1 is created by default.
- 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.
Note In Visual Studio 2005, you do not have to do step 2.
- Using the Toolbox, add a WebBrowser control, an OpenFileDialog control, and a Button control to Form1. This step adds the AxWebBrowser1 member variable, the OpenFileDialog1 member variable, and the Button1 member variable to the Form1 class.
- Define a private member in the Form1 class as follows.
Dim oDocument as Object
- Paste the following code in the Form1 class.Note You must change the code in Visual Studio 2005. By default, Visual Basic adds one form to the project when you create a Windows Forms project. The form is named Form1. The two files that represent the form are named Form1.vb and Form1.designer.vb. You write the code in Form1.vb. The Form1.designer.vb file is where the Windows Forms Designer writes the code that implements all the actions that you performed by dragging and dropping controls from the Toolbox.
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.
.FileName = ""
strFileName = .FileName
'If the user does not cancel, open the document.
If strFileName.Length Then
oDocument = Nothing
Private Sub Form1_Load(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
Button1.Text = "Browse"
.Filter = "Office Documents " & _
"(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt"
.FilterIndex = 1
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As _
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
oDocument = Nothing
Private Sub AxWebBrowser1_NavigateComplete2(ByVal sender As Object, _
ByVal e As AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event) _
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)
- 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 you use the WebBrowser controlYou 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 to Automate 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.Newer versions of Internet Explorer (5.0 and later) 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...
- 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:
Considerations when you use the WebBrowser control together with a 2007 Microsoft Office programBy default, the 2007 Office programs do not open Office documents in the Web browser. This behavior also affects the WebBrowser control. We recommended that you use a custom ActiveX document container instead of the WebBrowser control when you develop applications that open 2007 Office documents. For more information about custom ActiveX document containers, click the following article number to view the article in the Microsoft Knowledge Base:
ID do Artigo: 304643 - Última Revisão: 23 de mar de 2009 - Revisão: 1