I am working adding a field to a document and adding shading. The following code is not working.
With Selection
.Range.Font.Shading.BackgroundPatternColor = wdColorLightYellow
.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False, Text:="Page"
.Fields.Update
End With
'ActiveDocument.Fields(1).Select
'Selection.Range.Font.Shading.BackgroundPatternColor = wdColorLightYellow
The commented-out lines will add the shading to the first field in the document.
At the point you apply the shading there is nothing selected, the start and end points of the range are the same. Although in the UI you can apply formatting such as bold or italic before typing, applying shading would apply it to the entire paragraph.
To just shade the field it needs to be selected before applying the shading, as it is in the alternative code in your question.
With Selection
.Fields.add Range:=Selection.Range, Type:=wdFieldEmpty, _
PreserveFormatting:=False, text:="Page"
.Fields.Update
.MoveLeft Extend:=wdExtend
.Range.Font.Shading.BackgroundPatternColor = wdColorLightYellow
'this also works
'.Shading.BackgroundPatternColor = wdColorLightYellow
End With