Inserting Nested Fields

It is usually best to use ranges in VBA code, and avoid using selections. However, VBA does not allow you to create nested fields "on the fly" using ranges.
The best workaround is to store your frequently used nested fields as AutoText entries in an Add-in. The you can insert the Autotext entry using ranges:


Public Sub InsertNestedPageFieldInHeader() 
Dim objRange As Range
Dim sfolderpath As String

   Set objRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
   objRange.Collapse wdCollapseEnd

   sfolderpath = Application.Options.DefaultFilePath(wdStartupPath) & "\TemplateName.dot"

   Application.Templates(sfolderpath).AutoTextEntries("PagePlus1").Insert Where:=objRange, _
                                                                          RichText:=True
End Sub

However, if you really need to create the field dynamically you will have to use selections instead of ranges.

Public Sub InsertHyperLinkFieldWithinMacroButtonField() 

   ActiveWindow.View.ShowFieldCodes = True

   Selection.Fields.Add Range:=Selection.Range,
                        Type:=wdFieldType.wdFieldEmpty,
                        Text:= "HYPERLINK ""http://www.bettersolutions.com""",
                        PreserveFormatting:=False

   Selection.MoveLeft Unit:=wdUnits.wdCharacter, Count:=1, Extend:=wdExtend

   Selection.Fields.Add Range:=Selection.Range,
                        Type:=wdFieldType.wdFieldEmpty, _
                        PreserveFormatting:=False

   Selection.InsertAfter "MacroButton ""FollowLink"""

   ActiveWindow.View.ShowFieldCodes = False
   Selection.MoveRight Unit:=wdUnits.wdCharacter, Count:=1, Extend:=wdExtend

   Selection.Fields.Update
End Sub

You cannot use Selections in the Headers and Footers.
If you need to create a nested Headers or Footer field dynamically then create the field in the main document, and cut and paste it into the Headers or Footer:

Public Sub InsertNestedPageFieldInHeader() 
Dim objRange As Range

   ActiveWindow.View.ShowFieldCodes = True

'Insert dummy para at end of document
   ActiveDocument.Range.InsertAfter vbCr
   Set objRange = ActiveDocument.Range
   objRange.Collapse wdCollapseDirection.wdCollapseEnd
   objRange.Select

'Insert nested field
   Selection.Fields.Add Range:=Selection.Range,
                        Type:=wdFieldEmpty,
                        Text:= "PAGE ",
                        PreserveFormatting:=False

   Selection.MoveLeft Unit:=wdUnits.wdCharacter, Count:=1, Extend:=wdExtend

   Selection.Fields.Add Range:=Selection.Range,
                        Type:=wdFieldType.wdFieldEmpty, _
                        PreserveFormatting:=False

   Selection.TypeText Text:="= "
   Selection.MoveRight Unit:=wdUnits.wdCharacter, Count:=1, Extend:=wdExtend

   Selection.MoveRight Unit:=wdUnits.wdCharacter, Count:=1

   Selection.TypeText Text:=" + 1"
   Selection.MoveRight Unit:=wdUnits.wdCharacter, Count:=2

   Selection.MoveLeft Unit:=wdUnits.wdCharacter, Count:=1, Extend:=wdExtend

   Selection.Fields.Update
   ActiveWindow.View.ShowFieldCodes = False

'Cut field, delete dummy para mark, and paste field into header
   Selection.Cut
   ActiveDocument.Paragraphs.Last.Range.Delete

   Set objRange = ActiveDocument.Sections(1).Headers(wdHeaderFooterIndex.wdHeaderFooterPrimary).Range
           
   objRange.Collapse wdCollapseDirection.wdCollapseEnd
   objRange.Paste
End Sub

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