Microsoft Word Add-in Field Controls with code example

Microsoft Word Add-in Field Controls with code example

Field

Object refers to a components or controls in a document which gives user input options and can be manipulated with the help of code. With the help of Fields collection all fields can be iterated available in a document. In this post we will understand various methods and properties of field object.

Syntax

Document.Fields.Add(Range, Type, Text, [PreserveFormatting])

Parameters

  1. Range: required parameter, range type refers to range where field would be inserted
  2. Type: required parameter, refers to type of field that you would like to insert in the document. There are many predefined field constants which can be passed to Type parameter as some of listed below:
    • wdFieldAddressBlock
    • wdFieldAsk
    • wdFieldAuthor
    • wdFieldAutoNum
    • wdFieldBarCode
    • wdFieldBibliography
    • wdFieldCitation
    • wdFieldComments
  3. Text: optional parameter, refers to text which can be passed to identify the field.
  4. PreserveFormatting: optional parameter, if sets to true, determines to keep formatting.

Add

Public Sub AddAddressFields()
    'Declare object ot hold document
    Dim oDocument As Document
    'Declare object to hold range
    Dim oRange As Range
    
    'Bind active document reference
    Set oDocument = ActiveDocument
    
    'Bind selection
    Set oRange = Selection.Range
    
    'Field object
    Dim oAddressField As Field
    
    'Insert Address field
    Set oAddressField = oDocument.Fields.Add(Range:=oRange, Type:=wdFieldAddressBlock, Text:="Maharashtra, Inida")
    
    'Cleanup
    If Not oAddressField Is Nothing Then
        Set oAddressField = Nothing
    End If
    If Not oRange Is Nothing Then
        Set oRange = Nothing
    End If
    If Not oDocument Is Nothing Then
        Set oDocument = Nothing
    End If
End Sub

Output

Result

'Insert current date
    Set oDateField = oDocument.Fields.Add(Range:=oRange, Type:=wdFieldDate)
    
    'display Result
    MsgBox oDateField.Result

Output

Code

'Insert current date
    Set oDateField = oDocument.Fields.Add(Range:=oRange, Type:=wdFieldDate)
    
    'display code
    MsgBox "Date field Code is : " & oDateField.Code

Copy

'Date field
Dim oDateField As Field

'Insert current date
Set oDateField = oDocument.Fields.Add(Range:=oRange, Type:=wdFieldDate)

'Copy
oDateField.Copy
'New para
Selection.InsertParagraph
'move selection to new paragraph
Selection.Collapse direction:=wdCollapseEnd
'Paste
Selection.Paste

Output

Delete

'Date field
Dim oDateField As Field

'Delete all fields
For Each oDateField In oDocument.Fields
	oDateField.Delete
Next oDateField

Count

'Total fields
Debug.Print "You have total " &  oDocument.Fields.Count " Fields in the document"

DoClick

This method works for GO TO BUTTON whose type can be defined by wdFieldGoToButton constant which works like bookmark.

Code example

Public Sub ClickFieldExample()
    'Declare object ot hold document
    Dim oDocument As Document
    'Declare object to hold range
    Dim oRange As Range
    
    'Bind active document reference
    Set oDocument = ActiveDocument
    
    oDocument.Fields(1).DoClick
    
    'Cleanup
    If Not oDocument Is Nothing Then
        Set oDocument = Nothing
    End If
End Sub

Next >> Content Controls Microsoft Word VBA with example

Leave a Reply

Your email address will not be published. Required fields are marked *