VBA Code

link - https://gregmaxey.com/word_tip_pages/repeating_data.html


Fields Collection

This accessing the collection of all the fields in a document

Set objFields = objDocument.Fields 

ActiveDocument.Fields(1).Code.Text = "DATE \* MERGEFORMAT" 
ActiveDocument.Fields(1).Result.Text = "07/07/04"

Locking Fields

Locks all the fields in the collection

objFields.Locked = True 

If a field is locked it can no longer be updated

objField.Locked = True / False 

Sequence Numbering Fields with sub levels

'level 1
Selection.Fields.Add Range:=Selection.Range
                     Type:=wdFieldType.wdFieldEmpty
                     Text:="SEQ \L1 \*arabic \c \* MERGEFORMAT"
                     PreserveFormatting:=True

'level 2
                     Text:="SEQ \L2 \*alphabetic \c \* MERGEFORMAT"

ActiveDocument.TablesOfContents(1).UpdatePageNumbers 
ActiveDocument.TableOfContents.Update

Set objRange = objField.Code 
Set objRange = objField.Result

Displays the field codes

objField.ShowCodes = True / False 

objField.Type = wdFieldType.wdFieldType 
objField.Update
objField.Delete
objField.Code.Text
objField.Result.Text
objField.DoClick
objField.Kind = wdFieldKind.wdFiledKindNone

Selection.GoTo What:=wdGoToItem.wdGoToField, _ 
             Which:=wdGoToDirection.wdGoToNext, _
             Count:=1 , _
             Name:="Fieldname"

ActiveWindow.View.ShowFieldCodes = True / False 

Inserting Sequential Numbering

Selection.Fields.Add Range:=Selection.Range, 
                     Type:=wdFieldEmpty,
                     Text:="SEQ name \n",
                     PreserveFormatting:=True

Selection.Fields.Add Range:=Selection.Range,
                     Type:=wdFieldEmpty,
                     Text:="DATE \@ "dd-mmm-yyyy",
                     PreserveFormatting:=True

'adding the date
Selection.Collapse Direction:=wdCollapseDirection.wdcollapsestart
Set objField = ActiveDocument.Fields.Add(Range:=Selection.Range, _
                                         Type:=wdFieldType.wdfFeldDate)

'update in header and footers
Dim objSection As Section
For Each objSection In ActiveDocument.Sections
   sctSection.Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Fields.Update
   sctSection.Footers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Fields.Update
Next sctSection


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext