Microsoft Word Automation - Bookmarks

<< Click to Display Table of Contents >>

Navigation:  The Toolbox >

Microsoft Word Automation - Bookmarks

by Garry Robinson

 

This page shows a Class Module that you can use to pass data to a Word document.

The Word class module is a simple to use class module that is designed to get you started in the right way in your quest to automate Microsoft Word. This class module comes with all the source code so that you can take your software where you need to go.

 

Here is an example of the code that would run open an existing Word report using Visual Basic 6, add some text and print it out. The bolded text shows the class module in action.

 

Dim fileName As String, wordExport As FXc8_Word, successInt As Long

 

Set wordExport = New FXc8_Word

With wordExport

 

fileName = "c:\fxClasses\wordRep.doc"

successInt = .openWord(fileName)

 

If successInt = True Then

 

.gotoBookmark "Address1"

.addText "555 Willow Drive"

 

.gotoBookmark "Address2"

.addText "Sydney"

 

.gotoBookmark "State"

.addText "NSW"

 

.gotoBookmark "Postcode"

.addText "2000"

 

.gotoBookmark "Name"

.addText "John Smith"

 

.printDoc

.closeDoc

.Finished

 

Else

 

' Opening the file has failed

.Finished

 

End If

 

End With

 

What Do You Have To Do In Word ?

 

Take any word document and place it in a directory where all users of your software can reference it

Clean out any text that you are going to replace with your software

Add bookmarks to the word document where you are going to insert the text from your software.

Take a backup of the file to use as a template.

What Do You Have To Do To Add The Software To Your Application

Experiment with the sample application that you want to launch your Word report from. Working projects are provided for Access, Excel and Visual Basic.

Bring your word report into the test environment and add the bookmarks that you need.

Add the text that you want to add to the document

Now add the formfields or data extraction software so that you can display your data on your word report.

Move the sample application into your existing application.

 

What Actually Happens ?

 

In the Excel Example, you would have a worksheet that looks like example 1

wordauto1

 

Example 1 - A Worksheet that runs a Word Report using the data typed into the cells

 

When you click on the Print Report Command button, it runs the visual basic code and the class module to produce a word report that looks like example 2.

 

wordauto2

Example 2 - A Word document produced by the FX class module for Word Reports.

 

In the word report, the bookmarks are place markers that decide where the information is to be placed. The bookmarks do not appear on the document when it is printed.

 

What Tools Can You Use

Microsoft Access 2000 and beyond

Word 2000 and beyond

In your VBA project references, you need an additional reference to

Word X Object library

Your Sample Database Is Called   "wordrep.zip"

Sample database is suited to all versions of Access

 

 

Here is the Code for the Class Library

 

Create a Class Module, copy this in, save as FXc8_Word

 

Option Compare Database

Option Explicit

 

'fxC     --------------------------------------------------------

'fxC

'fxC-->  FXc8_Word ... Class library to act as a driver for Automating Microsoft Word

'fxC

'fxC     The calls to the class module defined below rely on the

'fxC     following declaration in the software that you are using

'fxC

'fxC     Public Word_FX As New FXc8_Word

'fxC

'fxC     So when the class module is called, the notes specify Word_FX rather than

'fxC     FXc8_Word

'fxC

'fxC     Requires a reference to the following non standard libraries

'fxC

'fxC     Word 8 Object library

'fxC

'fxC     Originally developed by GR-FX Pty Limited in Australia.

'fxC     Can be modified and used by customers who purchase the rights to the software

'fxC

'fxC     Oct 99      GR - Original

'fxC     -------------------------------------------------------------------------------

 

Public objWord As Word.Application

  

Public Function openWord(Optional DocumentFile) As Long

 

'fxC     --------------------------------------------------------

'fxC

'fxC-->  openWord ... Open a new Word File

'fxC

'fxC     This routine will open Word

'fxC     Usually you would also add the optional DocumentFile which would

'fxC     include a full file path specification.

'fxC

'fxC     eg     successInt = Word_FX.openWord("c:\my documents\letter.doc")

'fxC

'fxC     Oct 99      GR - Original

'fxC     -------------------------------------------------------------------------------

 

  On Error GoTo openWord_Err

         

  Set objWord = CreateObject("Word.Application")

  With objWord

    ' Make the application visible.

    .Visible = True

    ' Open the document.

    .Documents.Open DocumentFile

    

  End With

  openWord = True

  

openWord_Exit:

  Exit Function

 

openWord_Err:

         

  ' Display the current error and set the error number so that the software

  ' can be stopped

  

  MsgBox Err.Number & vbCr & Err.description

  openWord = Err.Number

 

End Function

 

Public Function gotoBookmark(nameStr As String)

  

'fxC     --------------------------------------------------------

'fxC

'fxC-->  gotoBookmark ... Goto a bookmark inside the word document

'fxC

'fxC     Prior to running this option, you need to add the bookmark to

'fxC     an existing word file at the location where you want to add new text

'fxC

'fxC     eg     Word_FX.gotoBookmark "Bookmark_15"

'fxC

'fxC     Oct 99      GR - Original

'fxC     -------------------------------------------------------------------------------

  

  objWord.ActiveDocument.Bookmarks(nameStr).Select

 

End Function

 

Public Function addText(txtStr As Variant)

    

'fxC     --------------------------------------------------------

'fxC

'fxC-->  addText ... Add text to the word file at the current location in the file

'fxC

'fxC     Usually you would find a location in an existing word file using using

'fxC     the gotoBookMark method and then add the text at that location.

'fxC

'fxC     eg     Word_FX.addText "My first word text"

'fxC

'fxC     Oct 99      GR - Original

'fxC     -------------------------------------------------------------------------------

    

  If Not IsNull(txtStr) Then

    objWord.Selection.Text = txtStr

  End If

  

End Function

 

Public Function printDoc()

  

'fxC     --------------------------------------------------------

'fxC

'fxC-->  printDoc ... Print the document out when you have finished adding text

'fxC

'fxC     When you have added all the text that you need to add to a document,

'fxC     this method will allow you to send it directly to the printer.

'fxC

'fxC     eg     Word_FX.printDoc

'fxC

'fxC     Oct 99      GR - Original

'fxC     -------------------------------------------------------------------------------

  

  objWord.ActiveDocument.PrintOut BackGround:=False

 

End Function

 

Public Function SaveAs(fileName As String)

 

'fxC     --------------------------------------------------------

'fxC

'fxC-->  SaveAs ... Save the currently opened document to a file

'fxC

'fxC     You will probably run this automatically after you have printed the document

'fxC     Closing the file without saving the changes using software will keep the

'fxC     integrity of the file and all the bookmarks etc.

'fxC

'fxC     eg     Word_FX.closeDoc

'fxC

'fxC     June 2007      GR - Original

'fxC     -------------------------------------------------------------------------------

  

  objWord.ActiveDocument.SaveAs fileName:=fileName

 

End Function

 

Public Function closeDoc()

 

'fxC     --------------------------------------------------------

'fxC

'fxC-->  closeDoc ... Close the document without saving the changes that you have made.

'fxC

'fxC     You will probably run this automatically after you have printed the document

'fxC     Closing the file without saving the changes using software will keep the

'fxC     integrity of the file and all the bookmarks etc.

'fxC

'fxC     eg     Word_FX.closeDoc

'fxC

'fxC     Oct 99      GR - Original

'fxC     -------------------------------------------------------------------------------

  

  objWord.ActiveDocument.close SaveChanges:=wdDoNotSaveChanges

 

End Function

 

Public Function Finished()

  

'fxC     --------------------------------------------------------

'fxC

'fxC-->  Finished ... Shut down word and removes the outstanding references to the object .

'fxC

'fxC     The method shuts Word down and removes the references to the class module

'fxC     that were initially set up.  Saves on computer resources.

'fxC

'fxC     eg     Word_FX.closeDoc

'fxC

'fxC     Oct 99      GR - Original

'fxC     -------------------------------------------------------------------------------

  

  objWord.Quit

  Set objWord = Nothing

 

End Function