WordFiller object – VBA Word document automation

Word document automation using VBA

VBA is the native programming language in Microsoft Office. As such, Word VBA is the common choice for automating Word document creation, filling a Word document with data from a data provider. In our case the data provider is Microsoft Access, either as the actual database or as intermediate to another data source.

VBA Word document automation: adding bookmarks for a table in word template designer
The Word Template Designer showing bookmarks where the document will be filled with data from the database.

The WordFiller object is used to fill or modify the content in bookmarked locations in a Word document. As you see on the left it provides an extensive set of high-level methods to implement all commonly used requirements. If you still find something missing, you can easily add it yourself or ask us to do it for you.

The WordFiller class module is added to your VBA project when you create an Word document automation process using Document Creation using Microsoft Access.

The first step in using WordFiller is to set the document it will be working on:


Dim doc As Word.Document
Set doc = appWord.Documents.Add(Template:=strTemplateFile, Visible:=True)
Dim wfr As New WordFiller: Set wfr.Document = doc

Bookmarks

With all below methods, the bookmark parameter accepts a string value identifying the Word Range to work on. Bookmarks normally correspond to the name of the field or bound control that provides the data. The BookmarkRange function provides a shortcut to the Range to operate on.

Methods

Adds a hyperlink on the bookmarked place


Public Sub AddHyperlink(Bookmark As String, Value As Variant, _
     Optional ParseValue As Boolean = False, _
     Optional TextToDisplay As String, _
     Optional SubAddress As String) 

ApplyBoolean

Applies the first available behaviour default: range contains text …

with checkbox set the checkbox checked/unchecked
(Optional text) Keep the text (brackets removed) or no text
with font strikethrough set strikethrough off/on
highlighted keep or remove highlight

Public Function ApplyBoolean(Bookmark As String, _
 Value As Boolean) As Word.Range

BookmarkRange

Returns the Word.Range object for a given bookmark. In addition to being the starting point for the methods implemented here, you can also use it as when writing your own document automation actions. If the bookmark supplied as argument in the function call does not exist an error is raised.


Function BookmarkRange(Bookmark As String) As Word.Range

Bookmark: string value indicating the Word bookmark.

FillElement

Inserts data at the bookmarked location.


Public Function FillElement(Bookmark As String, Value As String) As Word.Range

FillListFromArray

Fills a list with the supplied items in Array1D. In addition to the usual presentation formats (numbered, bulleted) for a list this method can also be used to fill a text range: a, b and c. To be able to control the exact result of this text range fill the optional parameters have been supplied.


Public Sub FillListFromArray(Bookmark As String, Array1D As Variant, _
  Optional Separator As String = ", ", Optional Final As String = " and ")

FillOLEObject

Fills the bookmarked location with an OLEObject, for example a Chart in an Excel file – possibly produced in another document creation process.


Public Sub FillOLEObject(Bookmark As String, FileName As String, 
  Optional LinktoFile As Boolean = False, Optional DisplayAsIcon As Boolean = False)

FillPicture

Fills the bookmark with a picture. The template Designer inserts a dummy image, which can be resized to indicate the intended size for the image to be inserted. By default, the image will be resized to have the same width as the dummy image in the template.

Other properties that are also copied from the dummy image are firstly its WrapFormat and also AlternativeText, Title, Flip, Hyperlink, PictureFormat and more.

The returned object is Shape or InlineShape.


Public Enum dcResizeMain
    NoResize = 0
    ResizeWidth = 1
    ResizeHeight = 2
End Enum
Public Function FillPicture(Bookmark As String, ImageFile As String, _
  Optional MainResize As dcResizeMain = ResizeWidth) As Object

FillPictureFromAttachment

Fill a picture location from a picture contained in an attachment control.


Public Function FillPictureFromAttachment(Bookmark As String, control As Attachment, _ 
       Optional MainResize As dcResizeMain = ResizeWidth) As Object

FillTableFromArray

Fills a Word.Table from a 2-dimensional array, starting at the row which contains the bookmark. If the input array, here obtained from and Access subform, has a different order of columns from the table to fill, this can be specified using ColumnOrder which is automatically done by the Template Designer.


Public Function FillTableFromArray(Bookmark As String, Array2D As Variant, 
Optional ColumnOrder As String) As Word.table

InsertFile

Inserts the content of a specified file on the bookmarked place. This allows you to maintain and use a collection of formatted text fragments from some folder on the network or the internet (OneDrive).


Public Sub InsertFile(Bookmark As String, FileName As String, _
  Optional Range As Variant, Optional ConfirmConversions = False, Optional Link = False)

SetCheckbox

Checks (True) or unchecks (False) the checkbox in the specified location. Note that each actual checkbox version is represented by two Symbols.

Returns a Word Range with a checkbox inside.


Public Function SetCheckbox(Bookmark As String, Value As Boolean, _
  Optional Extra As String) As Word.Range

Strikethrough

In case the Value is False, strikes through the string value on the bookmark location. If True possible Strikethrough formatting is removed.


Public Function Strikethrough(Bookmark As String, Value As Boolean) As Word.Range

UpdateAllFields

Updates any fields. This is particularly relevant because of the use of Fields in case a certain data item is applied more than once in the document. A bookmark name can only be used once in a document. If the same datum needs to be inserted in more locations, the Template Designer switches to using Fields with a reference to the bookmarked range. When a value is inserted using FillElement this does NOT automatically propagate to referencing fields. This is resolved by calling UpdateAllFields when the document filling is completed.


Public Sub UpdateAllFields()

UpdateAllTablesOfContents

Updates any TablesOfContents objects in the document. Usually at least the page numbers will need updating.


Public Sub UpdateAllTablesOfContents()

Properties

Document (Set/Get)

Set the Word Document on which the WordFiller methods will be applied. Makes the Word Document currently being filled available for additional actions.


Public Property Set Document(rData As Word.Document)
Public Property Get Document() As Word.Document