Skip to content
The Chat Archive
chatgpt-4o-latest-20250326· OpenAI LMArena Human Preference 140K

Sub UpdatepersianTOC() ' ' UpdatepersianTOC Macro ' ' Dim i As Integer, n As Integer…

  1. User

    Sub UpdatepersianTOC() ' ' UpdatepersianTOC Macro ' ' Dim i As Integer, n As Integer

    Application.ScreenUpdating = False
    
    
    
    
    n = ActiveDocument.TablesOfContents.count
    For i = 1 To n
        
        ActiveDocument.TablesOfContents(i).Update
        ActiveDocument.TablesOfContents(i).UpdatePageNumbers
        ActiveDocument.TablesOfContents(i).Range.Select
        selection.RtlPara
        selection.font.Name = "B lotus"
        selection.font.NameBi = "B lotus"
        selection.font.Italic = True
        selection.font.Italic = False
        selection.font.size = 13
        selection.font.Italic = False
        selection.font.ItalicBi = False
        selection.font.Italic = False
        selection.font.ItalicBi = False
        selection.font.SizeBi = 13
        selection.paragraphs().CloseUp
        selection.paragraphs().SpaceBefore = 0
        selection.paragraphs().SpaceAfter = 0
        If i > 1 Then
            selection.font.BoldBi = False
            selection.font.Bold = False
        End If
    Next i
    

    End Sub Please give me only procedure from both two above tasks after applying refactoring, dry, clean and elegent code of vba

  2. Assistant

    Here's a refactored, DRY, clean, and elegant version of your VBA UpdatepersianTOC procedure:

    Sub UpdatePersianTOC()
        Dim toc As TableOfContents
        Dim isFirst As Boolean: isFirst = True
    
        Application.ScreenUpdating = False
    
        For Each toc In ActiveDocument.TablesOfContents
            With toc
                .Update
                .UpdatePageNumbers
                With .Range
                    .Select
                    With Selection
                        .RtlPara
                        With .Font
                            .Name = "B lotus"
                            .NameBi = "B lotus"
                            .Size = 13
                            .SizeBi = 13
                            .Italic = False
                            .ItalicBi = False
                            If Not isFirst Then
                                .Bold = False
                                .BoldBi = False
                            End If
                        End With
                        With .Paragraphs
                            .CloseUp
                            .SpaceBefore = 0
                            .SpaceAfter = 0
                        End With
                    End With
                End With
            End With
            isFirst = False
        Next toc
    
        Application.ScreenUpdating = True
    End Sub
    

    Key Improvements:

    • Removed redundant or repeated lines (e.g., multiple .Italic = False).
    • Used For Each loop which is more elegant and readable for collections.
    • Renamed variables to meaningful names (toc, isFirst) for clarity.
    • Grouped similar formatting operations using With blocks.
    • Set Application.ScreenUpdating to True at the end for better UX.

    This version maintains functionality but is more maintainable and professional.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.