|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub 生成试题()
Dim t%, j%, k%, arr, wdApp, reg, mat
Set wdApp = CreateObject("Word.Application")
Dim wdD As Word.Document, aDoc As Word.Document
Set reg = CreateObject("VBScript.RegExp")
Set wdD = wdApp.Documents.Open(ThisWorkbook.Path & "\测验.doc")
arr = Range("A1").CurrentRegion
For i = 1 To UBound(arr, 1)
Set aDoc = wdApp.Documents.Add
For j = 2 To UBound(arr, 2)
If arr(i, j) = "" Then Exit For
With reg
.Global = True
.MultiLine = True
.Pattern = "^" & arr(i, j) & "(?:(?!\d+\.\d+).)+"
If .Test(wdD.Content) Then Set mat = .Execute(wdD.Content)
End With
wdD.Range(mat(0).FirstIndex, mat(0).FirstIndex + mat(0).Length).Copy
With aDoc.Range
If j > 2 Then
.Collapse wdCollapseEnd
' .InsertAfter (vbCrLf)
End If
.Paste
End With
Next
aDoc.SaveAs Filename:=ThisWorkbook.Path & "\" & arr(i, 1) & ".doc"
aDoc.Close
Next
wdD.Close
wdApp.Quit
Set wdD = Nothing
Set aDoc = Nothing
Set reg = Nothing
End Sub
|
|