|
本帖最后由 zhanglei1371 于 2018-9-11 11:45 编辑
花了一上午时间写的,可以用于从word题库中抽题。
优点在于可以保留特殊格式。如图片,上下标等都可以得以保存。
根据Excel的设定来随机抽取章节题目;
★★★为查找标志,必须有。
使用时同时打开word和execl,点击按钮即可。
源码参考:
- Sub 按Excel设定抽题()
- Dim ex As Object, wb As Object, sh As Object, arr, brr, crr
- Dim rg As Range, rg1 As Range, rg2 As Range, rgtmp As Range
- Dim re As Object, TF As Boolean
- Set re = CreateObject("VBSCRIPT.regexp")
- re.Global = 1
- re.MultiLine = 1
- re.Pattern = "^\d+."
- Set ex = GetObject(, "Excel.application")
- Set rg = ActiveDocument.Range
- For Each wb In ex.workbooks
- If InStr(wb.Name, "抽题") Then exname = wb.Name: Exit For
- Next
- For Each Doc In Documents
- If InStr(Doc.Name, "抽题") Then wdname = Doc.Name: Exit For
- Next
- Set docN = Documents.Add
- Set ADoc = Documents(wdname)
- Set sh = ex.workbooks(exname).worksheets(1)
- en_num = sh.Range("A635536").End(3).Row
- arr = sh.Range("A3:C" & en_num)
- For i = 1 To UBound(arr, 1)
- With rg.Duplicate.Find
- If .Execute("★★★" & arr(i, 1), , , 1) Then
- st = .Parent.Start
- .Parent.Collapse 0
- If .Parent.Find.Execute("★★★") Then
- Set rg1 = ADoc.Range(st, .Parent.End - 3)
- total_tishu = re.Execute(rg1).Count
- brr = 获取随机数(arr(i, 3), total_tishu)
- With rg1.Find
- For Each m In brr
- If .Execute("(^13)" & m & "." & "*参考答案[!^13]@^13", , , 1) Then
- If Not .Parent.InRange(rg1) Then MsgBox "查找的目标超出给定章节范围之外!": Exit Sub
- .Parent.MoveStart 1, 1
- docN.Bookmarks("\EndofDoc").Range.FormattedText = .Parent.FormattedText
- .Parent.MoveEnd 1, -1
- End If
- .Parent.Collapse 0
- Next
- End With
- End If
- End If
- End With
- Next
- Set ex = Nothing
- MsgBox "恭喜,已完成!"
- End Sub
- Function 获取随机数(a, b)
- Dim num()
- Randomize
- Do While n < CInt(a)
- ReDim Preserve num(n)
- temp = Int((b * Rnd) + 1)
- If n > 0 Then
- For i = 0 To UBound(num)
- If num(i) = temp Then
- TF = True
- Exit For
- End If
- Next
- End If
- If TF = False Then
- num(n) = temp
- n = n + 1
- End If
- TF = False
- Loop
- WordBasic.sortarray num
- 获取随机数 = num
- End Function
复制代码
附件:
|
评分
-
2
查看全部评分
-
|