Sub SetThesisHeadingStyles_Refactored()
' Sets formatting for Heading 1 through Heading 9 styles in a Word document.
' Implements linear font size decrease from START_SIZE to END_SIZE (20pt to 10pt).
' Applies font size to both standard (.Font.Size) and bidirectional (.Font.SizeBi) fonts.
' Adds distinct colors to each heading level from a predefined list.
' Keeps original paragraph formatting rules (line spacing, conditional space before/after, alignment).
' Assumes "IRNazanin" font is available and styles "Heading 1" to "Heading 9" exist in the document.
Dim i As Integer ' Loop counter for heading levels
Dim headingStyle As Style ' Object variable to hold the current style
Dim calculatedSize As Double ' Calculated font size for the current heading level
Dim styleName As String ' Name of the style being formatted
' --- Constants for Font Size Calculation ---
Const START_SIZE As Double = 20.0 ' Font size for Heading 1 (points)
Const END_SIZE As Double = 10.0 ' Font size for Heading 9 (points)
Const NUM_HEADINGS As Integer = 9 ' Total number of heading levels to format (1 to 9)
' Calculate the size decrement per heading level interval
' (Total size range) / (Number of intervals between headings 1 and 9)
' There are NUM_HEADINGS - 1 intervals (H1 to H2, H2 to H3, ..., H8 to H9)
Const DECREMENT_PER_LEVEL As Double = (START_SIZE - END_SIZE) / (NUM_HEADINGS - 1)
' --- Constants/Array for Colors ---
' Define the distinct colors for each heading level (H1 to H9)
' Using WdColor constants for standard Word colors
Dim headingColors(1 To NUM_HEADINGS) As WdColor
headingColors(1) = wdColorBlack ' Heading 1: Black
headingColors(2) = wdColorDarkBlue ' Heading 2: Dark Blue
headingColors(3) = wdColorGray25 ' Heading 3: Charcoal Gray (using 25% gray)
headingColors(4) = wdColorNavy ' Heading 4: Navy
headingColors(5) = wdColorDarkGreen ' Heading 5: Dark Green
headingColors(6) = wdColorBrown ' Heading 6: Burgundy (using Brown as closest WdColor constant)
headingColors(7) = wdColorGray80 ' Heading 7: Dark Slate Gray (using 80% gray)
headingColors(8) = wdColorMaroon ' Heading 8: Maroon
headingColors(9) = wdColorForestGreen ' Heading 9: Forest Green
' --- Constants for Paragraph Formatting ---
' (Keeping the original logic of using LinesToPoints(2) conditionally)
' Note: LinesToPoints calculates space based on the *current* document/paragraph context.
' Using a fixed point value (e.g., 24 for ~2 lines at 12pt) could be an alternative
' for more predictable spacing, but LinesToPoints(2) replicates the original logic.
' 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 dynamically
styleName = "Heading " & i
' Check if the style exists before attempting to modify it
If ActiveDocument.Styles.Exists(styleName) Then
' Get the style object
Set headingStyle = ActiveDocument.Styles(styleName)
' Apply formatting rules to the current style
With headingStyle
' Ensure style updates automatically based on manual changes (optional, as per original)
.AutomaticallyUpdate = True
' --- Font Formatting ---
.Font.Name = "IRNazanin" ' Set standard font name
.Font.NameBi = "IRNazanin" ' Set bidirectional font name
.Font.Bold = True ' Set standard font to bold
.Font.BoldBi = True ' Set bidirectional font to bold
' Set the distinct color for the current heading level using the array
.Font.Color = headingColors(i)
' Calculate the font size for the current heading level based on linear decrease
' Formula: StartSize - (Number of steps down from H1 * Decrement per step)
' For H1 (i=1), steps down = 0 -> Size = StartSize
' For H9 (i=9), steps down = 8 -> Size = StartSize - 8 * DecrementPerLevel = EndSize
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
' --- Paragraph Formatting ---
' Set line spacing to 1.5 lines
.ParagraphFormat.LineSpacingRule = wdLineSpace1_5
' Set space before and after paragraph based on heading level (as per original logic)
If i > 6 Then
' Apply 2 lines of space before and after for heading levels > 6
.ParagraphFormat.SpaceBefore = LinesToPoints(2)
.ParagraphFormat.SpaceAfter = LinesToPoints(2)
Else
' Explicitly set space to 0 for levels 1-6 to ensure consistency
.ParagraphFormat.SpaceBefore = 0
.ParagraphFormat.SpaceAfter = 0
End If
' Set paragraph alignment based on heading level (as per original logic)
If i = 9 Then
' Center align only Heading 9
.ParagraphFormat.Alignment = wdAlignParagraphCenter
Else
' Ensure other headings (1-8) are left aligned
.ParagraphFormat.Alignment = wdAlignParagraphLeft
End If
' Add any other specific paragraph formatting if needed (e.g., indents, WidowControl)
' Example: .ParagraphFormat.WidowControl = True
End With ' With headingStyle
Else
' Optional: Provide feedback if a standard heading style is unexpectedly missing
' Debug.Print "Warning: Style '" & styleName & "' does not exist in the document. Skipping."
End If
' Release the object reference to free memory for the next iteration
Set headingStyle = Nothing
Next i ' Move to the next heading level
' Re-enable screen updating after all styles are updated
Application.ScreenUpdating = True
' Optional success message
' MsgBox "Heading styles 1 to " & NUM_HEADINGS & " updated successfully with linear font size and colors.", vbInformation
Exit Sub ' Exit the sub successfully
' --- Error Handling ---
ErrorHandler:
' Ensure screen updating is turned back on in case of an error
Application.ScreenUpdating = True
' Display an error message
MsgBox "An error occurred while setting heading styles: " & Err.Description, vbCritical
End Sub
This refactored version is clearer, more maintainable due to the use of constants and the color array, and fulfills all the specified requirements.