please give me refactored VBA code to set the formatting for Heading 1 through Heading 9 styles, implementing the linear decrease in font size from 20pt to 10pt, applying it to both standard and bidirectional font sizes (bth .font.size and .font.SizeBi too), while keeping the other original formatting rules, using constants and clear logic for better readability and maintenance. Add distinct following Black
Dark Blue
Charcoal Gray
Navy
Dark Green
Burgundy
Dark Slate Gray
Maroon
Forest Green
to each heading too 

```vba
Sub SetThesisHeadingStyles()
    ' Sets formatting for Heading 1 through Heading 9 styles in a Word document.
    ' Font size decreases linearly from START_SIZE (Heading 1) to END_SIZE (Heading 9).
    ' Applies standard font properties and bidirectional font properties.
    ' Assumes "IRNazanin" font is available.
    Dim i As Integer
    Dim headingStyle As Style
    Dim calculatedSize As Double ' Use Double as size decrement might be fractional
    ' --- Constants for Font Size Calculation ---
    Const START_SIZE As Double = 20.0 ' Font size for Heading 1
    Const END_SIZE As Double = 10.0  ' Font size for Heading 9
    Const NUM_HEADINGS As Integer = 9 ' Total number of heading levels to format
    ' Calculate the size decrement per heading level
    ' (Total size range) / (Number of intervals between headings)
    Const DECREMENT_PER_LEVEL As Double = (START_SIZE - END_SIZE) / (NUM_HEADINGS - 1)
    ' --- Constants for Paragraph Formatting ---
    ' Define points for space before/after (equivalent to 2 lines based on assumed base font size)
    ' Using LinesToPoints(2) from original code, assumes document context.
    ' A fixed point value (e.g., 24 for 2 lines at 12pt) could also be used if preferred.
    Const SPACE_POINTS_AFTER_H6 As Long = 24 ' Example: LinesToPoints(2) might resolve to 24pt or similar
    ' Prevent screen flickering during style updates
    Application.ScreenUpdating = False
    ' Add basic error handling
    On Error GoTo ErrorHandler
    ' Loop through Heading styles from 1 to NUM_HEADINGS
    For i = 1 To NUM_HEADINGS
        ' Construct the style name
        Dim styleName As String
        styleName = "Heading " & i
        ' Check if the style exists before attempting to modify it
        If ActiveDocument.Styles.Exists(styleName) Then
            Set headingStyle = ActiveDocument.Styles(styleName)
            With headingStyle
                ' Ensure style updates automatically based on manual changes (useful during styling phase)
                .AutomaticallyUpdate = True
                ' --- Font Formatting ---
                .Font.Name = "IRNazanin"  ' Set font name
                ' Calculate the font size for the current heading level
                ' Formula: StartSize - (Number of steps down * Decrement per step)
                ' Number of steps down from Heading 1 is (i - 1)
                calculatedSize = START_SIZE - ((i - 1) * DECREMENT_PER_LEVEL)
                ' Apply the calculated size to both standard and bidirectional fonts
                .Font.Size = calculatedSize
                .Font.SizeBi = calculatedSize ' Apply same size to Bi font
                .Font.Bold = True       ' Set font to bold
                .Font.NameBi = "IRNazanin" ' Set bidirectional font name
                .Font.BoldBi = True     ' Set bidirectional font to bold
                ' --- Paragraph Formatting ---
                .ParagraphFormat.LineSpacingRule = wdLineSpace1_5 ' Set line spacing to 1.5 lines
                ' Set space before and after paragraph
                If i > 6 Then
                    ' Apply defined space for heading levels greater than 6
                    ' Using LinesToPoints(2) as in original code, or use a constant point value
                    .ParagraphFormat.SpaceBefore = LinesToPoints(2) ' Or use SPACE_POINTS_AFTER_H6
                    .ParagraphFormat.SpaceAfter = LinesToPoints(2)  ' Or use SPACE_POINTS_AFTER_H6
                Else
                    ' Explicitly set space to 0 for levels 1-6 to ensure consistency
                    .ParagraphFormat.SpaceBefore = 0
                    .ParagraphFormat.SpaceAfter = 0
                End If
                ' Set paragraph alignment
                If i = 9 Then
                    .ParagraphFormat.Alignment = wdAlignParagraphCenter ' Center align only Heading 9
                Else
                    ' Ensure other headings are left aligned (standard for most headings)
                    .ParagraphFormat.Alignment = wdAlignParagraphLeft
                End If
            End With
        Else
            ' Optional: Log or inform if a style like "Heading 7" doesn't exist (unlikely for 1-9)
            Debug.Print "Warning: Style '" & styleName & "' does not exist in the document."
        End If
        ' Release the object reference
        Set headingStyle = Nothing
    Next i ' Move to the next heading level
    ' Re-enable screen updating
    Application.ScreenUpdating = True
    ' Optional success message
    ' MsgBox "Heading styles 1 to " & NUM_HEADINGS & " updated successfully.", vbInformation
    Exit Sub ' Exit the sub before the error handler
ErrorHandler:
    ' Handle any errors that occurred
    Application.ScreenUpdating = True ' Ensure screen updating is turned back on
    MsgBox "An error occurred while setting heading styles: " & Err.Description, vbCritical
End Sub