Embedding and Automating Microsoft Word from Visual Basic

With Edraw office viewer component, it's easy for the vb developers to embed an existing word document in a form then do the word automating.

The developers can also programmatically generate the Word Document by using VB code then embed the MS Word document in a form. Not like other word read write component, Edraw office viewer component works with the full Microsoft Word program features. Almost all of the tasks which we perform on word can be done programmatically like Inserting Table of Contents, Linking documents, Mail Merge, Inserting Documents, Embedding documents, inserting pictures, watermark... etc.

If you are not familiar in embedding Edraw office component in Visual Bisic, you can refer to the following article firstly.

Embedding MS Word in VB.NET and Automating Word

Embed MS Excel in VB 6 and Do the Excel Automation

Starting off, the first step is to include the Word object references to the Solution. This can be done by right clicking the Reference Folder in the Solution explorer of the project and select Add Reference.

Add Office Reference

Browse Through the available COM objects and Select Microsoft Office 11.0 Object Library & Microsoft Word 11.0 Object Library. This Word DLL has all the methods which we do to perform the word automation.

Add Edraw Office Viewer Component in a Visual Basic Form.

add office viewer component

Free Download Office Viewer Component

Automating Microsoft Word in Visual Basic

The key point is how to build the relationship between the Edraw office component and the Microsoft Word Object Module.

In fact, the component provides two methods which return the Application and Document interface of the opened MS Word document. The developers can add the following Key Code in the DocumentOpened event of the component.

KEY CODE

Dim appWord As Word.Application
Set appWord = EDOffice1.GetApplication()
Dim oWordDoc As Word.Document
Set oWordDoc = EDOffice1.ActiveDocument()

Then the developers can do the word automation without any difference.

For Example:

Text Formatting

All the text formatting options available in the Word Application can also be replicated through automation.

oWordDoc.Selection.Font.Bold = 1
oWordDoc.Selection.Font.Color = Word.WdColor.wdColorAqua
oWordDoc.Selection.Font.Italic = 1
oWordDoc.Selection.Font.Underline = Word.WdUnderline.wdUnderlineDashHeavy

Embedding Pictures in Document Header

oWord.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader
Dim logoCustom As Word.Shape = Nothing
Dim logoPath As String = "C:\Document and Settings\MyLogo.jpg"
logoCustom = oWord.Selection.HeaderFooter.Shapes.AddPicture(logoPath, oFalse, oTrue, oMissing, oMissing, oMissing, oMissing, oMissing)
logoCustom.[Select](oMissing)
logoCustom.Name = "CustomLogo"
logoCustom.Left = CSng(Word.WdShapePosition.wdShapeLeft)
oWordDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument

Including Page Numbers in Page Footer

Including auto-generated page numbers in the Footer is yet another useful feature which can be simulated in the code.

oWordDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageFooter
oWordDoc.Selection.TypeParagraph()
Dim docNumber As String = "1"
Dim revisionNumber As String = "0"
oWordDoc.Selection.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft
oWordDoc.ActiveWindow.Selection.Font.Name = "Arial"
oWordDoc.ActiveWindow.Selection.Font.Size = 8
oWordDoc.ActiveWindow.Selection.TypeText("Document #: " + docNumber + " - Revision #: " + revisionNumber)
oWordDoc.ActiveWindow.Selection.TypeText("" & Chr(9) & "")
oWordDoc.ActiveWindow.Selection.TypeText("" & Chr(9) & "")
oWordDoc.ActiveWindow.Selection.TypeText("Page ")
Dim CurrentPage As Object = Word.WdFieldType.wdFieldPage
oWordDoc.ActiveWindow.Selection.Fields.Add(oWord.Selection.Range, CurrentPage, oMissing, oMissing)
oWordDoc.ActiveWindow.Selection.TypeText(" of ")
Dim TotalPages As Object = Word.WdFieldType.wdFieldNumPages
oWordDoc.ActiveWindow.Selection.Fields.Add(oWord.Selection.Range, TotalPages, oMissing, oMissing)
oWordDoc.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument

Inserting Text in the Centre of the Document as Water Mark

Dim logoWatermark As Word.Shape = Nothing
logoWatermark = oWordDoc.Selection.HeaderFooter.Shapes.AddTextEffect(Microsoft.Office.Core.MsoPresetTextEffect.msoTextEffect1, "Enter The Text Here", "Arial", CSng(60), Microsoft.Office.Core.MsoTriState.msoTrue, Microsoft.Office.Core.MsoTriState.msoFalse, 0, 0, oMissing)
logoWatermark.[Select](oMissing)
logoWatermark.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoTrue
logoWatermark.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
logoWatermark.Fill.Solid()
logoWatermark.Fill.ForeColor.RGB = DirectCast(Word.WdColor.wdColorGray30, Int32)
logoWatermark.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin
logoWatermark.RelativeVerticalPosition = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionMargin
logoWatermark.Left = CSng(Word.WdShapePosition.wdShapeCenter)
logoWatermark.Top = CSng(Word.WdShapePosition.wdShapeCenter)
logoWatermark.Height = oWordDoc.InchesToPoints(2.4F)
logoWatermark.Width = oWordDoc.InchesToPoints(6F)

Inserting Table of Contents

Dim oBookmarkTOC As Object = "Bookmark_TOC"
Dim rngTOC As Word.Range = oWordDoc.Bookmarks.get_Item(oBookmarkTOC).Range
rngTOC.[Select]()
Dim oUpperHeadingLevel As Object = "1"
Dim oLowerHeadingLevel As Object = "3"
Dim oTOCTableID As Object = "TableOfContents"
oWordDoc.TablesOfContents.Add(rngTOC, oTrue, oUpperHeadingLevel, oLowerHeadingLevel, oMissing, oTOCTableID, oTrue, oTrue, oMissing, oTrue, oTrue, oTrue)

Updating Table of Contents

oWordDoc.TablesOfContents(1).Update()
oWordDoc.TablesOfContents(1).UpdatePageNumbers()

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

Free Download Office Viewer Component and View Sample Projects

edraw max

FREE DOWNLOAD

Share the page:

Get Started! You Will Love This Easy-To-Use Diagram Software.

Edraw Max is perfect not only for professional-looking flowcharts, organizational charts, mind maps, but also network diagrams, floor plans, workflows, fashion designs, UML diagrams, electrical diagrams, science illustration, charts and graphs... and that is just the beginning!

Top