|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
以下代码兼顾填写和提取答案两项功能,是根据文档是第一个下划线格式文本是否为空格来智能判断的。处理时只需运行Main过程。当然,代码只是基于原附件文档,并没有普遍适用性,仅共参考。- Sub Main()
- '以文档第一个下划线格式文本是否为空格来判断提取
- With ActiveDocument.Content.Find
- .Font.Underline = wdUnderlineSingle
- .Format = True
- If .Execute Then If Trim(.Parent.Text) = Empty Then InsertAnswer Else GetAnswer
- End With
- End Sub
- Sub InsertAnswer()
- Dim a() As String, i As Integer
- Dim aa() As String, c As Integer, n As Integer
- Dim TF As Boolean, s As Long
- Dim myRange As Range
- Application.ScreenUpdating = False
- With ActiveDocument.Content.Find
- '搜集答案信息部分
- .Text = "填空题"
- .Forward = False
- .MatchWildcards = True
- If .Execute Then .Forward = True
- .Text = "^13[0-9]@.[!^13]{1,}"
- .Parent.Collapse wdCollapseEnd
- Do While .Execute
- ReDim Preserve a(i)
- a(i) = .Parent.Text
- a(i) = Trim(Mid(a(i), InStr(a(i), ".") + 1))
- i = i + 1
- If i > 60 Then Exit Do '填空题共有61题
- Loop
- .Parent.Collapse wdCollapseEnd
- Do While .Execute("[0-9]@.[∨×]")
- ReDim Preserve a(i)
- a(i) = .Parent.Text
- a(i) = Trim(Mid(a(i), InStr(a(i), ".") + 1))
- i = i + 1
- Loop
- .Parent.Collapse wdCollapseEnd
- Do While .Execute("[0-9]@.[A-F]{1,}")
- ReDim Preserve a(i)
- a(i) = .Parent.Text
- a(i) = Trim(Mid(a(i), InStr(a(i), ".") + 1))
- i = i + 1
- Loop
-
- '定位填写答案部分
- .Parent.WholeStory
- i = 0
- .Text = ""
- .Font.Underline = wdUnderlineSingle
- .Format = True
- Do While .Execute
- If .Parent.Text <> Chr(13) Then
- If TF = False Then
- aa = Split(a(i), Chr(32))
- TF = True
- End If
- For n = c To UBound(aa)
- c = c + 1
- If aa(n) <> Empty Then
- With .Parent
- .Text = Chr(32) & aa(n) & Chr(32)
- .Font.Color = wdColorRed
- .Bold = True
- End With
-
- If n = UBound(aa) Then
- i = i + 1
- c = 0
- TF = False
- End If
- Exit For
- End If
- Next
- End If
- .Parent.Collapse wdCollapseEnd
- Loop
- .Format = False
- .Text = "(^32{4,})"
- Do While .Execute
- With .Parent '一道选择题如有两个以上括号,答案只在第一个括号内
- If .Paragraphs.First.Range.Start > s Then
- .SetRange .Start + 2, .End - 2
- .Text = a(i)
- If a(i) Like "*[!A-F]*" = False Then
- Set myRange = .Duplicate
- myRange.Collapse wdCollapseEnd
- With myRange.Find
- .MatchWildcards = True
- .Replacement.Font.Bold = True
- .Replacement.Font.Color = wdColorRed
- For c = 1 To Len(a(i))
- .Text = Mid(a(i), c, 1) & "[..]*^13"
- .Execute Replace:=wdReplaceOne
- .Parent.Collapse wdCollapseEnd
- Next
- End With
- End If
- s = .Paragraphs.First.Range.Start
- i = i + 1
- End If
- .Collapse wdCollapseEnd
- End With
- Loop
- End With
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|