Embed Microsoft Word in VB.NET and Automating Word

Edraw office viewer component is an easy and reliable solution for the developers to Embed Microsoft Word documents in a vb.net application.

What is the best way of embedding a word document in a VB.Net application? I believe most of you remember the good old OLE technology which allowed Embedding Excel chart into Word documents. Or use the Office XP Primary Interpol Assemblies or Embed the WebBrowser control and navigate to the appropriate Office doc etc. But all these technologies do not support the full Microsoft Word features. It doesn't support multiple MS word instance in a computer. There are tons of known bugs in it.

Click Here to Download Office Viewer Component - Support vb6, vb.net project for Word, Excel, PowerPoint, Visio and Project

free download5 MB

The following article will show how to Embed a MS Word file in a VB.NET application step by step.

If you haven't the officeviewer.ocx file, you need to install the package firstly.

Embed Word Component in VB.NET

Open the Visual Studio and create a new VB.NET application.

Right Click the HostOffice Solution. Then click Add Reference... menu item.

VB.Net Add Reference

In the pop up dialog, choose the officeviewer.ocx file from the Browse tab.

add office viewer reference

Or choose the Edraw Office Viewer Component from the COM tab.

Click OK. The Edraw Office  Viewer Component References have been added in the new vb.net project.

vb reference

Switch to the Form design window of Form1.

Switch to the Form design window

Drag the Edraw Office Viewer Component from the Toolbox panel into the form1.

Add Word Component

Type the following VB.NET codes to new, open, saveas, close and print a word document look like this:

Public Class Form1
Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
If Dialog1.ShowDialog() Then
If Dialog1.GetChooseType() = 1 Then
ElseIf Dialog1.GetChooseType() = 2 Then
ElseIf Dialog1.GetChooseType() = 3 Then
ElseIf Dialog1.GetChooseType() = 4 Then
ElseIf Dialog1.GetChooseType() = 5 Then
End If
End If
End Sub
Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
End Sub
Private Sub btnSaveAs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAs.Click
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
End Sub
Private Sub btnPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPreview.Click
End Sub
Private Sub btnToolbars_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToolbars.Click
If AxEDOffice1.Toolbars = True Then
AxEDOffice1.Toolbars = False
AxEDOffice1.Toolbars = True
End If
End Sub
Private Sub btnAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbout.Click
End Sub
End Class

Open the Configuration Manager. Change the Active solution platform as x86 option.

x86 option.

Then build the VB.NET project and run.

The office viewer component support MS Word 97, Word2000, Word 2003, Word 2007 and Word 2010. It can run at the Windows 2000/Xp/Vista/2008/7 32 bit or 64 bit OS. To Embed MS Excel or PowerPoint, Visio, Project into a VB.NET application, you needn't change anything, only change the second parameter of the Open method as follows:

public void Open()
axEDOffice1.Open(sPath, "Excel.Application");
axEDOffice1.Open(sPath, "PowerPoint.Application");
axEDOffice1.Open(sPath, "Visio.Application");
axEDOffice1.Open(sPath, "MSProject.Application");

Automating Word in Office Viewer Component

Using COM to automate Word from your Visual Basic application isn't very hard with the word component. Add reference for Word Object Library 11.0 from add reference on solution explorer. Here I am using Word 2003, so object library version is 11.0.

The sample code below shows how to construct a minimal document, insert a bookmark and, at a later moment, replace the empty bookmark with some text.

Imports Microsoft.Office.Interop.Word

Private Sub Automating_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Automating.Click
Dim word = AxEDOffice1.GetApplication()
word.Visible = True
Dim doc As Document = AxEDOffice1.ActiveDocument()
Dim range As Range = doc.Range
range.InsertAfter("Range1" + vbCrLf)
doc.Bookmarks.Add("MijnBookmark", range)
range.InsertAfter("Range2" + vbCrLf)
Dim bookmark As Bookmark = doc.Bookmarks(1)
range = bookmark.Range
range.Text = "Bookmark" + vbCrLf
range.Font.Color = WdColor.wdColorBlue
End Sub

One thing to keep in mind when automating Word is the Word Macro Recorder. It is still there in Word 2007 but hidden away in the Developer Ribbon bar. Kind of strange as the macro recorder was always the simplest way of automating Word for the average user and they typically don't have the developer ribbon visible.

Embedding MS Office in ASP.NET Program

Embedding MS Project

Embedding Visio

An Easy Way to Embed Excel in a Web Page

Embedded MS PowerPoint

Disables MS Word Standard Command

Disable Office Ribbon Button

Show/Hide Office Menu Bar

Get Started! Make your application display and interact with MS Office. Try Office Viewer Component