|
楼主 |
发表于 2019-3-27 22:43
|
显示全部楼层
本帖最后由 小花鹿 于 2019-4-4 20:07 编辑
Sub 查找替换()
Dim fd, s1, s2 As Boolean, s3, t
t = Timer
Application.ScreenUpdating = False
Call DelOrRep
fd = "^.*?(?=[\u4e00-\u9fa5\w])" '查找内容
s1 = "" '替换前缀
s2 = 0 '替换时是否包含查找内容,0不包含1包含
s3 = "" '替换后缀
Call SubProgram(fd, s1, s2, s3)
'--------------------------------------------------------------------------
fd = "[A-F][、::..]|参考答案.{0,3}[::]|答案.{0,3}[::]" '查找内容
s1 = Chr(13) '替换前缀
s2 = 1 '替换时是否包含查找内容,0不包含1包含
s3 = "" '替换后缀
Call SubProgram(fd, s1, s2, s3)
'--------------------------------------------------------------------------
fd = "^\d.*?(单选题|多选题)" '查找内容
s1 = "" '替换前缀
s2 = 1 '替换时是否包含查找内容,0不包含1包含
s3 = "小花鹿" '替换后缀
Call SubProgram(fd, s1, s2, s3)
'--------------------------------------------------------------------------
fd = "(单选题|多选题)小花鹿" '查找内容
s1 = "" '替换前缀
s2 = 0 '替换时是否包含查找内容,0不包含1包含
s3 = "" '替换后缀
Call SubProgram(fd, s1, s2, s3)
'----------------------------------------------------------------------
Call DelOrRep
Call DelRow
Application.ScreenUpdating = True
MsgBox Timer - t
End Sub
Sub SubProgram(fd, s1, s2, s3)
Dim col As New Collection, k&, tm$
Dim para As Paragraph, j&, r&, mt, oRang As Range, n%, m%
With CreateObject("vbscript.regexp")
.Pattern = fd
.Global = True
.IgnoreCase = False
.MultiLine = True
For Each para In ActiveDocument.Paragraphs
For Each mt In .Execute(para.Range.Text)
k = k + 1
m = mt.FirstIndex
n = mt.Length
Set oRang = ActiveDocument.Range(para.Range.Start + m, para.Range.Start + m + n)
col.Add oRang, CStr(k)
Next
Next
End With
For j = 1 To col.Count
If s2 <> 0 Then tm = col(j)
col(j) = s1 & tm & s3 '替换为字符(根据自己需要)
Next
End Sub
Sub DelOrRep()
With ActiveDocument.Content.Find
.Execute "^11", , , 1, , , , 0, , "^p", 2 '软回车变为硬回车
.Execute "^13", , , 1, , , , 0, , "^p", 2 '有时光标可移到硬回车后面,可用这种方法解决
.Execute "^p^w", , , 0, , , , 0, , "^p", 2 '删除段前空白
.Execute "^w^p", , , 0, , , , 0, , "^p", 2 '删除段后空白
.Execute "^13{1,}", , , 1, , , , 0, , "^p", 2 '删除空行
End With
End Sub
Sub DelRow()
Dim i&, reg, s
Set reg = CreateObject("vbscript.regexp")
reg.Pattern = "^[0-9]+[、::..]|^[A-F][、::..]|^.{0,4}答案"
s = ActiveDocument.Range.Text
s = Split(s, Chr(13))
With ActiveDocument
For i = UBound(s) - 1 To 0 Step -1
If Not reg.test(s(i)) Then
.Paragraphs(i + 1).Range.Delete
End If
Next i
End With
End Sub
试题整理综合.rar
(69.02 KB, 下载次数: 12)
|
|