|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 weiyingde 于 2020-2-11 19:23 编辑
作为语文老师,常常要出学生默写题,出题方式基本是按照逗号、句号、问号、感叹号为间隔依据,划线设空,学生填写,此题就是为解决此种情况而设计的。如果你为语文老师,此代码一定能派上用场。
代码如下:
Sub 默写题随机划线()
Dim par As Paragraph
Dim dic As Object
'On Error Resume Next
Set RegEx = CreateObject("VBscript.RegExp")
RegEx.Pattern = "[^\d\..,,。!]+"
RegEx.Global = True
With ActiveDocument
With .Content.Font
.ColorIndex = wdAuto
.Underline = wdUnderlineNone
.Bold = False
End With
For Each par In .Paragraphs
Set dic = CreateObject("scripting.dictionary")
ssr$ = par.Range.Text
For Each mt In RegEx.Execute(Replace(ssr, Chr(13), ""))
n = n + 1
isr = n & "|" & mt.FirstIndex & "|" & mt.Length
dic(isr) = ""
Next
ky = dic.keys
m = Int(Rnd * n)
ft = VBA.Val(Split(ky(m), "|")(1))
lh = VBA.Val(Split(ky(m), "|")(2))
With .Range(par.Range.Start + ft, par.Range.Start + ft + lh).Font
.Name = "楷体"
.Bold = True
.ColorIndex = 9
.Underline = wdUnderlineWavy
End With
Set dic = Nothing
n = 0
Next
End With
End Sub
|
|