|
可试试如下代码(对原文本有要求),较复杂,只作字符处理,代码较长。- Option Explicit
- Dim answer As String
- Sub test()
- Dim i As Integer
- Dim oText As String
- Dim regEx As Object
- Dim Match As Object
- Dim data() As String '原数据
- Dim data2() As Variant '随机重排后数据
- Dim r As Variant
- Dim oDoc As Document
-
- Set oDoc = ActiveDocument
- oText = oDoc.Content.Text
- Set regEx = CreateObject("vbscript.regexp")
- With regEx
- .Global = True:
- .MultiLine = True
- .Pattern = "(^\d+)([\..、][^\r]*[\((])([A-Z])([\))]\r)(([A-Z][\..、][^\r]*\r)*)(【解析】[^\r]+\r)"
- For Each Match In .Execute(oText)
- ReDim Preserve data(8, i)
- data(0, i) = Match.Value
- data(1, i) = Match.Submatches(0) '题号
- data(2, i) = Match.Submatches(1) '
- data(3, i) = Match.Submatches(2) '答案
- data(4, i) = Match.Submatches(3)
- data(5, i) = Match.Submatches(4) '选项,假设每个选项单独一段
- data(6, i) = Match.Submatches(6) '解析,一个段落
- data(7, i) = myOptions(data(3, i), data(5, i), Replace(data(6, i), Chr(13), "")) '重排后选项与解释
- data(8, i) = answer '重排后答案
- i = i + 1
- Next
- End With
-
- r = Randomizing(UBound(data, 2))
- ReDim data2(UBound(r))
- For i = 0 To UBound(r)
- data2(i) = i + 1 & data(2, r(i)) & data(8, r(i)) & data(4, r(i)) & data(7, r(i))
- Next
-
- Documents.Add.Content.Text = Join(data2, vbCrLf)
- End Sub
- Function Explain(k As Variant, t As String) As Variant
- '处理解析文本。假设除选项字母外没有相同的单独选项字母
- Dim i As Integer
- Dim j As Integer
- Dim regEx As Object
- Dim Match As Object
- Dim sb() As Long
- Dim atext() As String
-
- Set regEx = CreateObject("vbscript.regexp")
- With regEx
- .Global = True:
- .MultiLine = True
- .Pattern = "(\b[A-Z]\b)([^A-Z\r]+)"
- For Each Match In .Execute(t)
- With Match
- For j = 0 To UBound(k)
- If Chr(j + 65) = .Submatches(0) Then
- t = Left(t, .FirstIndex - 1) & Replace(t, .Submatches(0), Chr(k(j) + 65), .FirstIndex, 1)
- ReDim Preserve sb(i)
- sb(i) = .FirstIndex
- Exit For
- End If
- Next
- End With
- i = i + 1
- Next
- End With
- For i = 0 To UBound(sb)
- ReDim Preserve atext(i)
- If i <> UBound(sb) Then atext(i) = Mid(t, sb(i) + 1, sb(i + 1) - sb(i)) Else atext(i) = Mid(t, sb(i) + 1)
- Next
- WordBasic.SortArray atext
- Explain = atext
- End Function
- Function Randomizing(n As Integer) As Variant
- '题号与选项顺序随机调整
- Dim c As Integer
- Dim i As Integer
- Dim d As Object
-
- Set d = CreateObject("Scripting.Dictionary")
- Randomize
- Do Until d.Count = n + 1
- c = Int(Rnd * (n + 1))
- d(c) = ""
- Loop
-
- Randomizing = d.Keys
- End Function
- Function myOptions(mydata1 As String, mydata2 As String, mydata3 As String) As String
- '选项与解析重排
- Dim c As Integer
- Dim i As Integer
- Dim n As Integer
- Dim d As Object
- Dim regEx As Object
- Dim t1() As String
- Dim t2() As String
- Dim k
-
- t1 = Split(mydata2, Chr(13))
- n = UBound(t1) - 1
- k = Randomizing(n)
- answer = Chr(k(Asc(mydata1) - 65) + 65)
- For i = 0 To UBound(k)
- t1(i) = Chr(k(i) + 65) & Mid(t1(i), 2)
- Next
- ReDim Preserve t1(UBound(k))
- WordBasic.SortArray t1
-
- t2 = Explain(k, mydata3)
- myOptions = Join(t1, Chr(13)) & vbCrLf & "【解析】" & Join(t2, "")
- End Function
复制代码 |
评分
-
1
查看全部评分
-
|