Here's the refactored VBA code with improved readability, constants for colors, and clear logic for setting heading styles with linearly decreasing font sizes:
Option Explicit
Sub SetThesisHeadingStyles()
' Sets formatting for Heading 1 through Heading 9 styles in a Word document
' with linearly decreasing font sizes and distinct colors for each heading
' --- Constants for Font Size Calculation ---
Const START_SIZE As Double = 20# ' Font size for Heading 1
Const END_SIZE As Double = 10# ' Font size for Heading 9
Const NUM_HEADINGS As Integer = 9 ' Total number of heading levels to format
' --- Constants for Paragraph Formatting ---
Const SPACE_AFTER_H6 As Long = 24 ' Space after for headings 7-9 (24pt = 2 lines at 12pt)
' --- Constants for Heading Colors ---
Const COLOR_BLACK As Long = RGB(0, 0, 0)
Const COLOR_DARK_BLUE As Long = RGB(0, 0, 128)
Const COLOR_CHARCOAL_GRAY As Long = RGB(64, 64, 64)
Const COLOR_NAVY As Long = RGB(0, 0, 128)
Const COLOR_DARK_GREEN As Long = RGB(0, 100, 0)
Const COLOR_BURGUNDY As Long = RGB(128, 0, 32)
Const COLOR_DARK_SLATE_GRAY As Long = RGB(47, 79, 79)
Const COLOR_MAROON As Long = RGB(128, 0, 0)
Const COLOR_FOREST_GREEN As Long = RGB(34, 139, 34)
' Array of colors for each heading level
Dim headingColors(1 To NUM_HEADINGS) As Long
headingColors(1) = COLOR_BLACK
headingColors(2) = COLOR_DARK_BLUE
headingColors(3) = COLOR_CHARCOAL_GRAY
headingColors(4) = COLOR_NAVY
headingColors(5) = COLOR_DARK_GREEN
headingColors(6) = COLOR_BURGUNDY
headingColors(7) = COLOR_DARK_SLATE_GRAY
headingColors(8) = COLOR_MAROON
headingColors(9) = COLOR_FOREST_GREEN
' Calculate the size decrement per heading level
Dim sizeDecrement As Double
sizeDecrement = (START_SIZE - END_SIZE) / (NUM_HEADINGS - 1)
' Prevent screen flickering during style updates
Application.ScreenUpdating = False
' Add error handling
On Error GoTo ErrorHandler
' Loop through Heading styles from 1 to NUM_HEADINGS
Dim i As Integer
For i = 1 To NUM_HEADINGS
Dim styleName As String
styleName = "Heading " & i
If Not ActiveDocument.Styles.Exists(styleName) Then
Debug.Print "Warning: Style '" & styleName & "' does not exist in the document."
GoTo NextHeading
End If
Dim headingStyle As Style
Set headingStyle = ActiveDocument.Styles(styleName)
With headingStyle
' Basic style properties
.AutomaticallyUpdate = True
' --- Font Formatting ---
.Font.Name = "IRNazanin"
.Font.NameBi = "IRNazanin"
' Calculate and set font size (linearly decreasing)
Dim currentSize As Double
currentSize = START_SIZE - ((i - 1) * sizeDecrement)
.Font.Size = currentSize
.Font.SizeBi = currentSize
' Font styles
.Font.Bold = True
.Font.BoldBi = True
' Set heading color
.Font.Color = headingColors(i)
.Font.ColorBi = headingColors(i)
' --- Paragraph Formatting ---
.ParagraphFormat.LineSpacingRule = wdLineSpace1_5
' Space before/after for headings 7-9
If i > 6 Then
.ParagraphFormat.SpaceBefore = SPACE_AFTER_H6
.ParagraphFormat.SpaceAfter = SPACE_AFTER_H6
Else
.ParagraphFormat.SpaceBefore = 0
.ParagraphFormat.SpaceAfter = 0
End If
' Alignment (only Heading 9 is centered)
If i = 9 Then
.ParagraphFormat.Alignment = wdAlignParagraphCenter
Else
.ParagraphFormat.Alignment = wdAlignParagraphLeft
End If
End With
NextHeading:
Set headingStyle = Nothing
Next i
' Re-enable screen updating
Application.ScreenUpdating = True
' Optional success message
' MsgBox "Heading styles 1 to " & NUM_HEADINGS & " updated successfully.", vbInformation
Exit Sub
ErrorHandler:
' Handle any errors that occurred
Application.ScreenUpdating = True
MsgBox "Error " & Err.Number & ": " & Err.Description & vbCrLf & _
"Occurred while setting style: " & styleName, vbCritical
End Sub