Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Aster-the-Med-Stu/0a42b589767a341842190abf73ad4204 to your computer and use it in GitHub Desktop.
Save Aster-the-Med-Stu/0a42b589767a341842190abf73ad4204 to your computer and use it in GitHub Desktop.
在 Word 里选中需要修正的文本内容,Alt + F11 打开宏面板,然后把下面这段粘贴进去运行即可~
Sub 下标修正()
Dim x, Y, j, k As Integer
Dim m, n As Long
Dim char As String
x = Selection.Start
Y = Selection.End
For j = x To Y Step 1
Selection.Start = j - 1
Selection.End = j
char = Selection.Text
n = InStr("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ))]", char)
If n <> 0 Then
Selection.Start = j
Selection.End = j + 1
char = Selection.Text
m = InStr("0123456789", char)
If m <> 0 Then
k = 2
Selection.Font.Subscript = wdToggle
Selection.Start = j
Selection.End = j + k
char = Selection.Characters(k).Text
m = InStr("0123456789-+", char)
Do While m <> 0
If char = "+" Or char = "-" Then
Selection.Start = j + k
Selection.End = j + k + 1
char = Selection.Text
m = InStr("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ(([", char)
If m <> 0 Then
GoTo LINE1
Else
Selection.Start = j - 1
Selection.End = j + k
char = Selection.Characters(1).Text
End If
If char = "O" Or char = "H" Then '与line1共同作用以防止将NH4+、AlO2-
Selection.Start = j
Selection.End = j + 1
Selection.Font.Subscript = wdToggle
Selection.Font.Subscript = wdToggle
Selection.Start = j + 1
Selection.End = j + k
Selection.Font.Superscript = wdToggle
GoTo LINE1
End If
Selection.Start = j
Selection.End = j + k
Selection.Font.Superscript = wdToggle
Selection.Font.Superscript = wdToggle
GoTo LINE1
End If
k = k + 1
Selection.Start = j
Selection.End = j + k
char = Selection.Characters(k).Text
If char = "-" Or char = "+" Then
Selection.Start = j
Selection.End = j + k - 2
Selection.Font.Subscript = wdToggle
Selection.Font.Subscript = wdToggle
Selection.Start = j + k - 2
Selection.End = j + k
Selection.Font.Superscript = wdToggle
GoTo LINE1
Else
m = InStr("0123456789", char)
End If
Loop
Selection.Start = j
Selection.End = j + k - 1
Selection.Font.Subscript = wdToggle
Selection.Font.Subscript = wdToggle
End If
LINE1:
Selection.Start = j
Selection.End = j + 1
char = Selection.Text
m = InStr("-", char)
If m <> 0 Then
Selection.Start = j + 1
Selection.End = j + 2
char = Selection.Text
m = InStr("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ(([", char)
If m = 0 Then
Selection.Start = j
Selection.End = j + 1
Selection.Font.Superscript = wdToggle
End If
End If
m = InStr("+", char)
If m <> 0 Then
Selection.Start = j + 1
Selection.End = j + 2
char = Selection.Text
m = InStr("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ(([+", char)
If m = 0 Then
Selection.Start = j
Selection.End = j + 1
Selection.Font.Superscript = wdToggle
End If
End If
End If
Next j
Selection.Start = Y
Selection.End = Y
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment