Embed Word in VB 6 and Automating Word

> Edraw Tip > Embed Word in VB 6 and Automating Word
author
Posted by James Freeman |
With Edraw office viewer component, it's easy for the vb developers to embed an existing MS Word document in a form then do the word automating.

At form load event the developers can use the Open method to load the *.docx, *.doc, *.dot, *.dotx, *.rtf or *.txt files from the hard drive or server and open in an area on the form. The MS Word file can be read-only, password protected, full edited, or prohibit to save or print by the different requirement. The developers can also do the Word automating easily with the word component.

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

free download5 MB

Embed Word Component in VB 6 Form

The following article will demo how to Embed the MS Word Window in a VB form step by step.

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

Start Visual Basic and create a new standard project. Form1 is created by default.

In the Toolbox panel, right click the blank region. Then click the Component...

vb 6 add components...

In the pop up dialog, check the Edraw Office Viewer Component.

vb6 choose component

Click the Ok button.

The Office Viewer Component was added in the Toolbar window.

EDOffice Toolbox

Switch to a VB 6 form, then add the component in it.

vb6 embed excel component

The component includes lots of methods, events and properties to customize the Excel Window.

The developer can use the following code to open an excel file in the Form_Load Event.

Private Sub Form_Load()
EDOffice1.OpenFileDialog
'EDOffice1.OpenWord "d:\test.docx"
'EDOffice1.Open "d:\test.doc", "Word.Application"
End Sub

To protect the Word document from modification by the end user, the developer needs add the following code in the DocumentOpened event.

Private Sub EDOffice_DocumentOpened()
EDOffice1.ProtectDoc 2 'wdAllowOnlyFormFields
End Sub

There are some wrapped word automation method available to create or modify word data.

bool [id(58), helpstring("Shows/Hides the revisions for the office program.")]
VARIANT_BOOL WordShowRevisions([in] VARIANT_BOOL Show);
[id(59), helpstring("Accepts or rejects all the tracked changes in a document or range. ")]
VARIANT_BOOL WordAcceptAllRevisions([in] VARIANT_BOOL Receipt);
[id(60), helpstring("Returns the number of bookmarks.")]
long WordGetBookmarkCount();
[id(61), helpstring("Returns the name or value of the special bookmark.")]
BSTR WordReadBookmarkInfo([in] long Pos, [in] VARIANT_BOOL NameOrValue);
[id(62), helpstring("Writes the value for the special bookmark.")]
VARIANT_BOOL WordWriteBookmarkInfo([in] BSTR Name, [in] BSTR Value);
[id(63), helpstring("Inserts a file to opened Word file.")]
VARIANT_BOOL WordInsertFile([in] BSTR FilePath, [in, optional] VARIANT InDocPos);
[id(64), helpstring("Inserts text content to opened Word file.")]
VARIANT_BOOL WordInsertText([in] BSTR Text, [in, optional] VARIANT InDocPos);
[id(65), helpstring("Inserts a picture to opened Word file.")]
VARIANT_BOOL WordInsertPicture([in] BSTR FilePath, [in] VARIANT_BOOL InlineObject, [in, optional] VARIANT InDocPos);
[id(66), helpstring("Inserts a break to opened Word file.")]
VARIANT_BOOL WordInsertBreak([in] WdBreakType BreakType);
[id(67), helpstring("Goes to the specified item in the Word document.")]
VARIANT_BOOL WordGotoItem([in] WdGoToItem What, [in] WdGoToDirection Which, [in, optional] VARIANT Count, [in, optional] VARIANT Name);
[id(68), helpstring("Replaces all the specified string value with another string value.")]
VARIANT_BOOL WordReplaceText([in] BSTR Text, [in] BSTR ReplaceText, [in] VARIANT_BOOL MatchWholeWord, [in] VARIANT_BOOL MatchCase);
[id(69), helpstring("Compares and merges documents then displays it in the current windows.")]
VARIANT_BOOL WordMergeAndCompare([in] BSTR TargetFilePath);
[id(70), helpstring("Disables drag and drop.")]
VARIANT_BOOL WordDisableDragAndDrop(VARIANT_BOOL Disable);
[id(71), helpstring("Returns the number of Revisions.")]
long WordGetRevisionCount();
[id(72), helpstring("Returns the name or value of the special bookmark.")]
BSTR WordReadRevisionInfo([in] long Pos, [in] WdRevision RevType);
[id(73), helpstring("Accepts or rejects the specified tracked change.")]
VARIANT_BOOL WordAcceptRevision([in] long Pos, [in] VARIANT_BOOL Accept);
[id(74), helpstring("Copies the whole content to the clipboard.")]
VARIANT_BOOL WordCopyToClipboard();
[id(75), helpstring("Copies the whole content to the clipboard as picture.")]
VARIANT_BOOL WordCopyToClipboardAsPicture();
[id(76), helpstring("Pastes to the opened Word file from the clipboard data.")]
VARIANT_BOOL WordPasteFromClipboard([in, optional] VARIANT InDocPos);
[id(77), helpstring("Pastes to the opened Word file from the clipboard data with special format.")]
VARIANT_BOOL WordPasteSpecialFromClipboard([in] WdPasteDataType lFormatType, [in] VARIANT_BOOL vFloatOverText, [in, optional] VARIANT InDocPos);
[id(78), helpstring("Returns the current page index in the word.")]
long WordGetCurrentPageIndex();
[id(79), helpstring("Returns the total page counts in the word.")]
long WordGetPageCount();

Manipulate MS Word by using Automation from Visual Basic

Click Project and then click References. The References dialog box appears. Scroll down the list until you find Microsoft Word object library (Microsoft Word xx.x Object Library), and then select the item to add a reference to Word. If the correct object library for your version of Word does not appear in the list, make sure that you have your version of Word properly installed.

Click OK to close the References dialog box.

Add a CommandButton to Form1.

In the code window for Form1, insert the following code:

Private Sub Command1_Click()

Dim oWd As Word.Application
Set oWd = EDOffice1.GetApplication()
Dim oDialogs As Word.Dialogs
Set oDialogs = oWd.Dialogs
Dim oDialog As Word.Dialog
Set oDialog = oDialogs.Item(112)
oDialog.Show
End Sub

Press F5 to run the project. The Word automation will open the file find/replace window.

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 Excel in VB 6

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

download EdrawMind
main page

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

EdrawMax is an advanced all-in-one diagramming tool for creating professional flowcharts, org charts, mind maps, network diagrams, UML diagrams, floor plans, electrical diagrams, science illustrations, and more. Just try it, you will love it!