You may want to display, or embed, a Microsoft Office document
directly on a Microsoft Visual Basic form. Microsoft Visual Basic 2005 and Visual Basic .NET do not provide an OLE control that lets you embed an
Office document in a form. If you want to embed an existing document and open
it as an in-place ActiveX document object within a Visual Basic form, a
potential solution for you is to use the
WebBrowser control.
This article demonstrates how to browse to an
existing Office document and display it in a Visual Basic form by
using the
WebBrowser control.
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 in 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 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 documents
To 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.
- 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
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. - 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 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 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.
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")
Considerations when you use the WebBrowser control together with a 2007 Microsoft Office program
By 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:
311765Â
(http://kbalertz.com/Feedback.aspx?kbNumber=311765/
)
Visual C++ ActiveX control for hosting Office documents in Visual Basic or HTML
For existing applications that require backward compatibility with the
WebBrowser control, you can modify the registry to configure Internet Explorer. You can use this method to configure Internet Explorer to open 2007 Office documents in the Web browser.
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
927009Â
(http://kbalertz.com/Feedback.aspx?kbNumber=927009/
)
A new window opens when you try to view a 2007 Microsoft Office program document in Windows Internet Explorer 7
Note If you modify the registry by using the method that is mentioned in Knowledge Base article 927009, the changes affect the
WebBrowser control that you use in the application. The changes also affect all instances of Internet Explorer. Additionally, this method may not work for any future versions of the Microsoft Office suites. Therefore, we recommend that you use this method only for compatibility with a existing application.
For more information about how to use the WebBrowser control, click the following article numbers to view the articles in the Microsoft Knowledge Base:
304562Â
(http://kbalertz.com/Feedback.aspx?kbNumber=304562/
)
Visual Studio 2005 and Visual Studio .NET do not provide an OLE container control for Windows Forms
243058Â
(http://kbalertz.com/Feedback.aspx?kbNumber=243058/
)
How to use the WebBrowser control to open an Office document
162719Â
(http://kbalertz.com/Feedback.aspx?kbNumber=162719/
)
How to use the WebBrowser control from Visual Basic 5.0
202476Â
(http://kbalertz.com/Feedback.aspx?kbNumber=202476/
)
BUG: Cannot edit Word document in OLE or WebBrowser control
188271Â
(http://kbalertz.com/Feedback.aspx?kbNumber=188271/
)
How to print contents of the WebBrowser control from VB
191692Â
(http://kbalertz.com/Feedback.aspx?kbNumber=191692/
)
Shdocvw.dll is not included in PDW setup package
238313Â
(http://kbalertz.com/Feedback.aspx?kbNumber=238313/
)
Accessing the Internet Explorer Document Object Model from Visual Basic
927009Â
(http://kbalertz.com/Feedback.aspx?kbNumber=927009/
)
A new window opens when you try to view a 2007 Microsoft Office program document in Windows Internet Explorer 7