View Single Post
 
Old 09-17-2021, 09:36 AM
Shelley Lou Shelley Lou is offline Windows 10 Office 2016
Competent Performer
 
Join Date: Dec 2020
Posts: 170
Shelley Lou is on a distinguished road
Default VBA Formatting Fields within parentheses

Andrew, I think I've fixed the problem with the italics, it still debugs on one of my testing documents but I can live with that for that particular document, I just added aFld.Code.Italic = True and it now works

Code:
Sub SetFieldFormat()
  Dim aFld As Field, sCode As String
  For Each aFld In ActiveDocument.Fields
    sCode = LCase(aFld.Code)
    If sCode Like "* ref *" Then
      Debug.Print sCode
      If aFld.Result.Words.First.Previous = "(" Then
        Debug.Print aFld.Result.Words.First.Previous
        If sCode Like "*mergeformat*" Then
          sCode = Replace(sCode, "mergefield", "charfield")
        ElseIf Not sCode Like "*charformat*" Then
          sCode = sCode & " \* charformat "
          aFld.Code.Text = sCode
          aFld.Code.Italic = True
        End If
        aFld.Update
      End If
    End If
  Next aFld
End Sub
Reply With Quote