Attribute VB_Name = "modBracketsV4"
Option Explicit

' Bracket resizer v4 (stays open, remembers position).
' Import into the "Normal" project, run: ShowBracketsDialogV4
Private Const FRM_NAME As String = "frmBracketsV4"

Public Sub ShowBracketsDialogV4()
    Dim f As Object
    On Error GoTo EH
    For Each f In VBA.UserForms
        If f.Name = FRM_NAME Then
            f.Show 0
            Exit Sub
        End If
    Next f
    EnsureBracketsForm
    Set f = VBA.UserForms.Add(FRM_NAME)
    f.Show 0
    Exit Sub
EH:
    MsgBox ChrW(1513) & ChrW(1490) & ChrW(1497) & ChrW(1488) & ChrW(1492) & ": " & Err.Description & vbCrLf & vbCrLf & _
        ChrW(1489) & ChrW(1508) & ChrW(1506) & ChrW(1501) & " " & ChrW(1492) & ChrW(1512) & ChrW(1488) & ChrW(1513) & ChrW(1493) & ChrW(1504) & ChrW(1492) & " " & ChrW(1497) & ChrW(1513) & " " & ChrW(1500) & ChrW(1488) & ChrW(1508) & ChrW(1513) & ChrW(1512) & ": " & vbCrLf & _
        "File > Options > Trust Center > Trust Center Settings > Macro Settings" & vbCrLf & _
        "-> Trust access to the VBA project object model", vbExclamation
End Sub

Public Function StyleListV4() As Variant
    Dim col As New Collection, st As Style, arr() As String, i As Long
    col.Add "(" & ChrW(1499) & ChrW(1500) & " " & ChrW(1492) & ChrW(1505) & ChrW(1490) & ChrW(1504) & ChrW(1493) & ChrW(1504) & ChrW(1493) & ChrW(1514) & ")"
    For Each st In ActiveDocument.Styles
        If st.Type = wdStyleTypeParagraph And st.InUse Then col.Add st.NameLocal
    Next st
    ReDim arr(0 To col.Count - 1)
    For i = 1 To col.Count
        arr(i - 1) = col(i)
    Next i
    StyleListV4 = arr
End Function

Private Sub EnsureBracketsForm()
    Dim vbp As Object, comp As Object, d As Object, c As Object, s As String
    Dim i As Integer, caps As Variant
    Set vbp = NormalTemplate.VBProject

    On Error Resume Next
    vbp.VBComponents.Remove vbp.VBComponents("frmBracketsAuto")
    vbp.VBComponents.Remove vbp.VBComponents("frmBracketsV2")
    vbp.VBComponents.Remove vbp.VBComponents("frmBracketsV3")
    Set comp = vbp.VBComponents(FRM_NAME)
    On Error GoTo 0
    If Not comp Is Nothing Then Exit Sub

    Set comp = vbp.VBComponents.Add(3)
    comp.Name = FRM_NAME
    comp.Properties("Caption") = ChrW(1492) & ChrW(1511) & ChrW(1496) & ChrW(1504) & ChrW(1514) & " " & ChrW(1505) & ChrW(1493) & ChrW(1490) & ChrW(1512) & ChrW(1497) & ChrW(1497) & ChrW(1501)
    comp.Properties("Width") = 250
    comp.Properties("Height") = 320
    comp.Properties("StartUpPosition") = 1
    Set d = comp.Designer

    Set c = d.Controls.Add("Forms.Label.1", "lblType")
    c.Caption = ChrW(1505) & ChrW(1493) & ChrW(1490) & " " & ChrW(1505) & ChrW(1493) & ChrW(1490) & ChrW(1512) & ChrW(1497) & ChrW(1497) & ChrW(1501) & ":"
    c.Left = 10: c.Top = 8: c.Width = 225: c.Height = 14: c.TextAlign = 3

    caps = Array("( )   " & ChrW(1506) & ChrW(1490) & ChrW(1493) & ChrW(1500) & ChrW(1497) & ChrW(1501), "[ ]   " & ChrW(1502) & ChrW(1512) & ChrW(1493) & ChrW(1489) & ChrW(1506) & ChrW(1497) & ChrW(1501), "{ }   " & ChrW(1502) & ChrW(1505) & ChrW(1493) & ChrW(1500) & ChrW(1505) & ChrW(1500) & ChrW(1497) & ChrW(1501), _
                 "< >   " & ChrW(1502) & ChrW(1513) & ChrW(1493) & ChrW(1500) & ChrW(1513) & ChrW(1497) & ChrW(1501), ChrW(1499) & ChrW(1500) & " " & ChrW(1505) & ChrW(1493) & ChrW(1490) & ChrW(1497) & " " & ChrW(1492) & ChrW(1505) & ChrW(1493) & ChrW(1490) & ChrW(1512) & ChrW(1497) & ChrW(1497) & ChrW(1501))
    For i = 0 To 4
        Set c = d.Controls.Add("Forms.OptionButton.1", "optType" & i)
        c.Caption = caps(i)
        c.GroupName = "grpType"
        c.Left = 20: c.Top = 26 + i * 20: c.Width = 210: c.Height = 18
    Next i

    Set c = d.Controls.Add("Forms.Label.1", "lblSize")
    c.Caption = ChrW(1490) & ChrW(1493) & ChrW(1491) & ChrW(1500) & " " & ChrW(1492) & ChrW(1505) & ChrW(1493) & ChrW(1490) & ChrW(1512) & ChrW(1497) & ChrW(1497) & ChrW(1501) & " (" & ChrW(1504) & ChrW(1511) & ChrW(1493) & ChrW(1491) & ChrW(1493) & ChrW(1514) & "):"
    c.Left = 10: c.Top = 134: c.Width = 225: c.Height = 14: c.TextAlign = 3

    Set c = d.Controls.Add("Forms.TextBox.1", "txtSize")
    c.Left = 170: c.Top = 152: c.Width = 45: c.Height = 20
    c.Text = "8"

    Set c = d.Controls.Add("Forms.SpinButton.1", "spnSize")
    c.Left = 217: c.Top = 152: c.Width = 16: c.Height = 20
    c.Min = 1: c.Max = 72: c.Value = 8

    Set c = d.Controls.Add("Forms.Label.1", "lblStyle")
    c.Caption = ChrW(1492) & ChrW(1495) & ChrW(1500) & " " & ChrW(1506) & ChrW(1500) & " " & ChrW(1505) & ChrW(1490) & ChrW(1504) & ChrW(1493) & ChrW(1503) & ":"
    c.Left = 10: c.Top = 184: c.Width = 225: c.Height = 14: c.TextAlign = 3

    Set c = d.Controls.Add("Forms.ComboBox.1", "cboStyle")
    c.Left = 20: c.Top = 202: c.Width = 210: c.Height = 20
    c.Style = 2

    Set c = d.Controls.Add("Forms.CheckBox.1", "chkContent")
    c.Caption = ChrW(1492) & ChrW(1511) & ChrW(1496) & ChrW(1503) & " " & ChrW(1490) & ChrW(1501) & " " & ChrW(1488) & ChrW(1514) & " " & ChrW(1492) & ChrW(1514) & ChrW(1493) & ChrW(1499) & ChrW(1503) & " " & ChrW(1513) & ChrW(1489) & ChrW(1514) & ChrW(1493) & ChrW(1498) & " " & ChrW(1492) & ChrW(1505) & ChrW(1493) & ChrW(1490) & ChrW(1512) & ChrW(1497) & ChrW(1497) & ChrW(1501)
    c.Left = 20: c.Top = 234: c.Width = 210: c.Height = 18
    c.Value = True

    Set c = d.Controls.Add("Forms.CommandButton.1", "btnOK")
    c.Caption = ChrW(1492) & ChrW(1495) & ChrW(1500)
    c.Left = 140: c.Top = 264: c.Width = 85: c.Height = 24
    c.Default = True

    Set c = d.Controls.Add("Forms.CommandButton.1", "btnCancel")
    c.Caption = ChrW(1505) & ChrW(1490) & ChrW(1493) & ChrW(1512)
    c.Left = 30: c.Top = 264: c.Width = 85: c.Height = 24
    c.Cancel = True

    s = "Private Sub UserForm_Initialize()" & vbCrLf & _
        "    Dim v As Variant, i As Long, sl As String" & vbCrLf & _
        "    optType0.Value = True" & vbCrLf & _
        "    v = StyleListV4()" & vbCrLf & _
        "    For i = LBound(v) To UBound(v)" & vbCrLf & _
        "        cboStyle.AddItem v(i)" & vbCrLf & _
        "    Next i" & vbCrLf & _
        "    cboStyle.ListIndex = 0" & vbCrLf & _
        "    sl = GetSetting(""BracketsMacro"", ""Form"", ""Left"", """")" & vbCrLf & _
        "    If sl <> """" Then" & vbCrLf & _
        "        Me.StartUpPosition = 0" & vbCrLf & _
        "        Me.Left = CSng(sl)" & vbCrLf & _
        "        Me.Top = CSng(GetSetting(""BracketsMacro"", ""Form"", ""Top"", ""0""))" & vbCrLf & _
        "    End If" & vbCrLf & _
        "End Sub" & vbCrLf
    s = s & _
        "Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)" & vbCrLf & _
        "    SaveSetting ""BracketsMacro"", ""Form"", ""Left"", CStr(Me.Left)" & vbCrLf & _
        "    SaveSetting ""BracketsMacro"", ""Form"", ""Top"", CStr(Me.Top)" & vbCrLf & _
        "End Sub" & vbCrLf & _
        "Private Sub spnSize_Change()" & vbCrLf & _
        "    txtSize.Text = CStr(spnSize.Value)" & vbCrLf & _
        "End Sub" & vbCrLf
    s = s & _
        "Private Sub btnOK_Click()" & vbCrLf & _
        "    Dim idx As Long, i As Long, sName As String" & vbCrLf & _
        "    For i = 0 To 4" & vbCrLf & _
        "        If Me.Controls(""optType"" & i).Value Then idx = i" & vbCrLf & _
        "    Next i" & vbCrLf & _
        "    If cboStyle.ListIndex > 0 Then sName = cboStyle.Text" & vbCrLf & _
        "    ApplyBracketsV4 idx, txtSize.Text, chkContent.Value, sName" & vbCrLf & _
        "End Sub" & vbCrLf & _
        "Private Sub btnCancel_Click()" & vbCrLf & _
        "    Unload Me" & vbCrLf & _
        "End Sub" & vbCrLf
    comp.CodeModule.AddFromString s
End Sub

Private Sub SetBracketSize(r As Range, ByVal sz As Single)
    r.Font.Size = sz
    r.Font.SizeBi = sz
End Sub

Private Sub ResizeSpans(rng As Range, ByVal o As String, ByVal c As String, _
                        ByVal sz As Single, ByVal styleName As String)
    Dim para As Paragraph, pr As Range, t As String, ch As String
    Dim k As Long, depth As Long, st As Long
    For Each para In rng.Paragraphs
        If Len(styleName) = 0 Or para.Range.ParagraphStyle.NameLocal = styleName Then
            Set pr = para.Range
            If pr.Start < rng.Start Then pr.Start = rng.Start
            If pr.End > rng.End Then pr.End = rng.End
            t = pr.Text
            depth = 0
            For k = 1 To Len(t)
                ch = Mid$(t, k, 1)
                If ch = o Then
                    If depth = 0 Then st = k
                    depth = depth + 1
                ElseIf ch = c And depth > 0 Then
                    depth = depth - 1
                    If depth = 0 Then
                        SetBracketSize rng.Document.Range(pr.Start + st - 1, pr.Start + k), sz
                    End If
                End If
            Next k
        End If
    Next para
End Sub

Public Function ApplyBracketsV4(ByVal idx As Long, ByVal sizeText As String, _
                                ByVal withContent As Boolean, ByVal styleName As String) As Boolean
    Dim sz As Single, opens As String, closes As String, chars As String
    Dim i As Long, rng As Range, r As Range

    If Not IsNumeric(sizeText) Then
        MsgBox ChrW(1497) & ChrW(1513) & " " & ChrW(1500) & ChrW(1492) & ChrW(1494) & ChrW(1497) & ChrW(1503) & " " & ChrW(1490) & ChrW(1493) & ChrW(1491) & ChrW(1500) & " " & ChrW(1514) & ChrW(1511) & ChrW(1497) & ChrW(1503) & " (" & ChrW(1502) & ChrW(1505) & ChrW(1508) & ChrW(1512) & ").", vbExclamation
        Exit Function
    End If
    sz = CSng(sizeText)
    If sz < 1 Or sz > 400 Then
        MsgBox ChrW(1492) & ChrW(1490) & ChrW(1493) & ChrW(1491) & ChrW(1500) & " " & ChrW(1495) & ChrW(1497) & ChrW(1497) & ChrW(1489) & " " & ChrW(1500) & ChrW(1492) & ChrW(1497) & ChrW(1493) & ChrW(1514) & " " & ChrW(1489) & ChrW(1497) & ChrW(1503) & " 1 " & ChrW(1500) & "-400.", vbExclamation
        Exit Function
    End If

    Select Case idx
        Case 0: opens = "(": closes = ")"
        Case 1: opens = "[": closes = "]"
        Case 2: opens = "{": closes = "}"
        Case 3: opens = "<": closes = ">"
        Case Else: opens = "([{<": closes = ")]}>"
    End Select
    chars = opens & closes

    If Selection.Type = wdSelectionIP Then
        Set rng = ActiveDocument.Content
    Else
        Set rng = Selection.Range
    End If

    Application.ScreenUpdating = False
    Application.UndoRecord.StartCustomRecord ChrW(1492) & ChrW(1511) & ChrW(1496) & ChrW(1504) & ChrW(1514) & " " & ChrW(1505) & ChrW(1493) & ChrW(1490) & ChrW(1512) & ChrW(1497) & ChrW(1497) & ChrW(1501)

    If withContent Then
        For i = 1 To Len(opens)
            ResizeSpans rng, Mid$(opens, i, 1), Mid$(closes, i, 1), sz, styleName
        Next i
    End If

    For i = 1 To Len(chars)
        Set r = rng.Duplicate
        With r.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            If Len(styleName) > 0 Then .Style = styleName
            .Text = Mid$(chars, i, 1)
            .Replacement.Text = "^&"
            .Replacement.Font.Size = sz
            .Replacement.Font.SizeBi = sz
            .Forward = True
            .Wrap = wdFindStop
            .Format = True
            .MatchWildcards = False
            .Execute Replace:=wdReplaceAll
        End With
    Next i

    Application.UndoRecord.EndCustomRecord
    Application.ScreenUpdating = True
    ApplyBracketsV4 = True
End Function
