|
本帖最后由 duquancai 于 2016-11-30 22:06 编辑
按照一楼附件做的,下载附件只需打开“题目”这个文档,进入VBE编辑器,执行如下代码!!!- Sub 插入答案()
- Dim col As New Collection, k%, j%, str$, x&, y&
- Dim mt, mh, oRang As Range, aRang As Range, n&, m&
- Dim TMdoc As Document, DAdoc As Document
- Application.ScreenUpdating = False
- Set TMdoc = ThisDocument
- Set DAdoc = Documents.Open(ThisDocument.Path & "\答案.docx", Visible:=False)
- With CreateObject("vbscript.regexp")
- .Pattern = "^\d+\.(?:(?!^\d+\.|^[一二]|/\r).)+"
- .Global = True: .IgnoreCase = False: .MultiLine = True
- For Each mt In .Execute(DAdoc.Content)
- k = k + 1: m = mt.FirstIndex: n = mt.Length
- Set oRang = DAdoc.Range(m, m + n)
- oRang.MoveStart 1, 2: oRang.MoveEnd 1, -1: col.Add oRang, CStr(k)
- Next
- End With
- m = 0: n = 0: k = 0
- str = Replace(TMdoc.Content, Chr(7), "")
- With CreateObject("vbscript.regexp")
- .Pattern = "^\d+\.(?:(?!^\d+\.|^[一二]|知识点|选择题).)+"
- .Global = True: .IgnoreCase = False: .MultiLine = True
- For Each mh In .Execute(str)
- k = k + 1: m = mh.FirstIndex: y = mh.Length
- Set aRang = TMdoc.Range(m + x, m + x + y)
- With aRang
- If aRang.Tables.Count > 0 Then
- .Collapse: .MoveEndUntil Chr(7): .MoveEnd 1, -1
- .InsertAfter Chr(13): .Collapse 0
- .FormattedText = col(k).FormattedText: .Font.ColorIndex = 6
- Else
- .InsertAfter Chr(13): .Collapse 0: .MoveEnd 1, -1
- .FormattedText = col(k).FormattedText: .Font.ColorIndex = 6
- End If
- End With
- n = Len(col(k).Text) + 1: x = x + n
- Next
- End With
- DAdoc.Close 0
- Application.ScreenUpdating = True
- End Sub
复制代码
|
|