|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
以下代码是对6楼代码的调整,亦可用于对有制表符分隔的选项及没有解析文本的题目,只作了简单测试- Dim answer As String
- Sub test2()
- '随机重排题目与选项。题目每个选项可单独一段,或以制表符分隔
- Dim i As Integer
- Dim j 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\t]*[\r\t])+)(【解析】[^\r]+\r)?"
- For Each Match In .Execute(oText)
- ReDim Preserve data(8, i)
- For j = 0 To 4
- data(j + 1, i) = Match.Submatches(j)
- Next
- 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 = Replace(Join(data2, vbCrLf), Chr(13) & Chr(13), Chr(13))
- MsgBox "共随机重排了" & i & "题。"
- End Sub
- Function Randomizing(n As Integer) As Variant
- '题号与选项顺序随机调整
- Dim c 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 i As Integer
- Dim n As Integer
- Dim t1() As String
- Dim t2() As String
- Dim k
-
- Do While InStr(mydata2, vbTab & vbTab) > 0
- mydata2 = Replace(mydata2, vbTab & vbTab, vbTab)
- Loop
- If InStr(mydata2, vbTab) > 0 Then mydata2 = Replace(mydata2, Chr(13), vbTab)
- t1 = Split(mydata2, IIf(InStr(mydata2, vbTab) = 0, Chr(13), Chr(9)))
- n = UBound(t1) - 1
- k = Randomizing(n)
- answer = Chr(k(Asc(mydata1) - 65) + 65)
- For i = 0 To UBound(k)
- t1(i) = Replace(Chr(k(i) + 65) & Mid(t1(i), 2), Chr(13), "")
- Next
- ReDim Preserve t1(UBound(k))
- WordBasic.SortArray t1 '排序
-
- If mydata3 <> Empty Then t2 = Explain(k, mydata3)
- myOptions = Join(t1, IIf(InStr(mydata2, vbTab) = 0, Chr(13), Chr(9))) & IIf(mydata3 <> Empty, vbCrLf & "【解析】", "") & Join(t2, "")
- End Function
- 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\t]+)"
- 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
复制代码 |
评分
-
3
查看全部评分
-
|