Archive for November, 2010

InsertFieldAfter Function in Word VBA

November 4, 2010

I wanted a function in Word VBA that would add a Field to a Range’s Fields collection and automatically extend the Range to include the added field.

This mimics the behavior of the Range.InsertAfter function and similar functions.

It allows you to keep adding text, fields, and other items to a Word document sequentially.

The function I created is called InsertFieldAfter. Its parameters mimic the parameters to the Fields.Add function.

The Range you pass in is extended to include the newly added Field. The new Range is also returned as the function return value for convenience.

The text of the function and related support functions is below, followed by an example of how to use the function.