Using Addin Fields

Article contributed by Hak-lok NG

Addin fields are a bit like Private fields – they don't display a result when field codes are switched off. But they are more private than Private fields are, because they don't display their Data value even when field codes are switch on. That is, they have a Data value that is hidden from the user interface and which can only be set and read using VBA. So in a way, they are quite similar to Document Variables (whereas you might say that Private fields are quite similar to Document Properties) – the former cannot be modified via the user interface, the latter can (and the same pros and cons apply in both cases).

For some reason, Addin fields are not documented directly in Help (and they are not mentioned on the Microsoft Knowledge Base either!); but they are indirectly documented, in the Help topic on the Data property of the Field object, which says:

Returns or sets data in an ADDIN field. Read/write String.

Note The data is not visible in the field code or result; it is only accessible by returning the value of the Data property. If the field isn't an ADDIN field, this property will cause an error.

The Help topic has a See also, button, which takes you to the topic on the Addin object – but if there's any relationship between an Addin field and the Addin object, it is not documented.

There are various reasons why you might wish to use Addin fields, for example:

1.

It is portable, unlike a Document Variable – if you paste the text elsewhere, the field is pasted with it.

2.

It can be nested in a MacroButton field to simulate passing an argument to a macro – see Using MacroButton fields.

The simplest way to create an Addin field is to press Ctrl+F9 to insert the field braces {}, and then type Addin inside the braces. To set its value, select it and run the code:

Selection.Fields(1).Data = "My Addin field text"

To read its value, you can use:

MsgBox Selection.Fields(1).Data

If you wanted to insert it programmatically you could use:

Dim oField As Field, MyRange As Range
Set MyRange = [wherever, e.g. Selection.Range]
Set oField = ActiveDocument.Fields.Add (Range:=MyRange, _
        Type:=wdFieldAddin)
oField.Data = "My Addin field text"


Click to view Terms of Use page

Click to view Disclaimer page