|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub main_批量上标test()
- '------------------------------------上面这部分主要是把原文字和上下标存入数组
- Dim doc As Document, n%, Str_Original() As String, Str_Superscript() As String, Str_Subscript() As String
- Set doc = ActiveDocument: n = 1
- ReDim Preserve Str_Original(1 To n)
- ReDim Preserve Str_Superscript(1 To n) '上标
- ReDim Preserve Str_Subscript(1 To n) '下标
- Str_Original(1) = "KAl(SO4)2": Str_Superscript(1) = "AL": Str_Subscript(1) = "4,2" '下标不同时用逗号隔开
- 'Str_Original(2) = "KAl(SO4)2": Str_Superscript(2) = "AL": Str_Subscript(2) = "4,2"
- 'Str_Original(3) = "KAl(SO4)2": Str_Superscript(3) = "AL": Str_Subscript(3) = "4,2"
- '如果少的话,可以自己往后加
- '------------------------------------下面这部分主要遍历组数,用查找替换的方式分别对字符串上下标
- Dim Str_Start&, Str_End&, Rng As Range, Str_Superscript_Arr() As String, Str_Subscript_Arr() As String
- For i = 1 To UBound(Str_Original)
- Call findstr_批量上标(Str_Original(i), Str_Superscript(i), Str_Subscript(i))
- Next i
- End Sub
- Sub findstr_批量上标(findStr$, Str_Super$, Str_Sub$)
- Dim doc As Document, Str_Start&, Str_End&, Str_Superscript_Arr() As String, Str_Subscript_Arr() As String
- Set doc = ActiveDocument
- With doc.Content.Find
- .Text = findStr
- Do While .Execute
- Str_Start = .Parent.Start: Str_End = .Parent.End
- If Str_Super <> "" Then
- Str_Superscript_Arr = Split(Str_Super, ",")
- For j = 0 To UBound(Str_Superscript_Arr)
- Call set上标_批量上标(Str_Start, Str_End, Str_Superscript_Arr(j))
- Next j
- End If
- If Str_Sub <> "" Then
- Str_Subscript_Arr = Split(Str_Sub, ",")
- For j = 0 To UBound(Str_Subscript_Arr)
- Call set下标_批量上标(Str_Start, Str_End, Str_Subscript_Arr(j))
- Next j
- End If
- Loop
- End With
- End Sub
- Sub set上标_批量上标(S_Start&, S_End&, S_Super$)
- Dim doc As Document, Rng As Range
- Set doc = ActiveDocument: Set Rng = doc.Range(S_Start, S_End)
- With Rng.Find
- .Text = S_Super
- .Replacement.Font.Superscript = True
- .Execute Replace:=wdReplaceAll
- End With
- End Sub
- Sub set下标_批量上标(S_Start&, S_End&, S_Sup$)
- Dim doc As Document, Rng As Range
- Set doc = ActiveDocument: Set Rng = doc.Range(S_Start, S_End)
- With Rng.Find
- .Text = S_Sup
- .Replacement.Font.Subscript = True
- .Execute Replace:=wdReplaceAll
- End With
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|