|
- Sub mate()
- Dim path$, i%, j%, n%, t$, sty As Boolean
- path = ThisDocument.path
- Documents.Open path & "\题目答案配对.doc"
- Documents.Open path & "\题目的答案.doc"
- i = 1: j = 1
- Do
- Windows("题目的答案").Activate
- If j = 1 Then Selection.HomeKey Unit:=wdStory '找第1个i、时才置于文首
- With Selection.Find
- .ClearFormatting
- .Text = "^13(" & i & "、)*^13"
- .Forward = True
- .MatchWildcards = True
- If .Execute Then
- t = Mid(Selection, 2)
- Selection.MoveRight Unit:=wdCharacter, Count:=1
- Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=1 '答案包含多段
- Do
- temp = Selection.Text
- If temp = Chr(13) Then Exit Do
- sty = Left(Selection.Style, 2) = "标题" '该段为标题级别
- Selection.MoveRight Unit:=wdCharacter, Count:=1
- If temp = "" Or temp = Chr(13) Or sty Then '至文末
- n = i + 1
- Else
- n = InStr(temp, "、")
- If n Then '与工作表函数不同,不能用n=IIf(n <> 0, Val(Left(temp, n - 1)), 0),当n=0时也进入非当前分支会校验表达式Val(Left(temp, n - 1))的
- n = Val(Left(temp, n - 1))
- End If
- End If
- If n <> i + 1 Then
- t = t & temp
- Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=1
- Else
- Selection.MoveUp Unit:=wdParagraph, Count:=1
- Exit Do
- End If
- Loop
- Else
- If j = 1 Then
- Exit Do '本次的第1个i、就不存在,可以终结查找
- Else
- i = i + 1
- j = 1
- GoTo nexti '第j个i、不存在,准备找i+1、,此时待找的为第1个i+1.
- End If
- End If
- End With
- Windows("题目答案配对").Activate
- If j = 1 Then Selection.HomeKey Unit:=wdStory
- With Selection.Find '切换到另一文档时,查找参数不能沿用上一文档的,而且with……endwith也许独立出来
- .ClearFormatting
- .Text = "^13(" & i & "、)*^13"
- .Forward = True
- .MatchWildcards = True
- .Execute
- sty = False
- Do
- Selection.MoveRight Unit:=wdCharacter, Count:=1
- Selection.MoveDown Unit:=wdParagraph, Count:=1, Extend:=1
- If Left(Selection.Style, 2) = "标题" Then
- sty = True
- Else
- temp = Selection.Text
- n = InStr(temp, "、")
- If n Then
- sty = Val(Left(temp, n - 1)) = i + 1
- End If
- End If
- Loop Until sty Or temp = Chr(13) '光标移至下题首或“标题”级别或文末前
- Selection.MoveUp Unit:=wdLine, Count:=1
- Selection.EndKey Unit:=wdLine
- Selection.InsertAfter Chr(13) & t
- Selection.MoveDown Unit:=wdParagraph, Count:=3 '避免插入的i、影响查找下一个
- End With
- j = j + 1 '下一个为第j个i、
- nexti:
- Loop
- Windows("题目的答案").Activate
- ActiveWindow.Close False
- End Sub
复制代码
|
|