|
楼主 |
发表于 2023-3-27 09:41
|
显示全部楼层
本想等个想在现成的,没想到使用的人少,我就自己将它解决了。以下代码要求必须是答案位于试卷的最后(不是题后,这个另有代码),答案开始部位必须以“{【参考答案】}”标记。由于时间匆忙,请大家指正。
Sub AfterKeytoEndNote()
With ActiveDocument.Content.Find '将软回车替换为硬回车
.ClearFormatting
With .Replacement
.ClearFormatting
End With
.Execute FindText:="^l", ReplaceWith:="^p", Format:=True, Replace:=wdReplaceAll
End With
With ActiveDocument.Content.Find '去掉一个空格,该空格的前面不为空格或英文字符,该空格后不为空格且不包含在域中
.ClearFormatting
.Font.Underline = WdUnderline.wdUnderlineNone
.MatchWildcards = True
With .Replacement
.ClearFormatting
End With
.Execute FindText:="([!^32 a-zA-Z])([ ]{1})([!^32 ][!eq])", ReplaceWith:="\1\3", Format:=True, Replace:=wdReplaceAll
End With
With ActiveDocument.Content.Find '规范小数的正确写法
.ClearFormatting
.MatchWildcards = True
With .Replacement
.ClearFormatting
End With
.Execute FindText:="([0-9]{1,2})([.、])([(\(【一-龥])", ReplaceWith:="\1.\3", Format:=True, Replace:=wdReplaceAll
End With
With ActiveDocument.Content.Find '规范小数的正确写法
.ClearFormatting
.MatchWildcards = True
With .Replacement
.ClearFormatting
End With
.Execute FindText:="([^13])([0-9]{1,2})([,,.、·])([!0-9])", ReplaceWith:="\1\2.\4", Format:=True, Replace:=wdReplaceAll
End With
With ActiveDocument.Content.Find '规范小数的正确写法
.ClearFormatting
.MatchWildcards = True
With .Replacement
.ClearFormatting
End With
.Execute FindText:="([0-9])(.)([0-9])", ReplaceWith:="\1.\3", Format:=True, Replace:=wdReplaceAll
End With
With ActiveDocument.Content.Find '规范小数的正确写法
.ClearFormatting
.MatchWildcards = True
With .Replacement
.ClearFormatting
End With
.Execute FindText:="([0-9])([.])([0-9]{4}[年])", ReplaceWith:="\1.\3", Format:=True, Replace:=wdReplaceAll
End With
With ActiveDocument.Content.Find '将连续回车替换为一个回车
.ClearFormatting
With .Replacement
.ClearFormatting
End With
.Execute FindText:="^p^p", ReplaceWith:="^p", Format:=True, Replace:=wdReplaceAll
End With
Dim KeyParagraphs As Integer
ActiveDocument.ActiveWindow.Selection.HomeKey Unit:=wdStory
With ActiveDocument.ActiveWindow.Selection.Find
.ClearFormatting
.Text = "{【参考答案】}"
.Forward = True
.Execute
If .Found = False Then
MsgBox "没有找到关键字" & """" & "{【参考答案】}" & """" & "的位置,请标注。", MsgBoxStyle.Information, "注意:"
Exit Sub
End If
End With
KeyParagraphs = ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Paragraphs.Count
Dim m As Integer
Dim n As Integer
Dim i As Integer
Dim c As Integer
Dim ktStartp As Integer
Dim keyStartp As Integer
Dim nextkeyStartp As Integer
Dim ktNo As Integer
Dim KeyTxt As String
Dim keyStartRange As Range
Dim myParagraphs As Integer
Dim str As String
nextkeyStartp = KeyParagraphs
Dim TotalKT As Integer
For i = 1 To ActiveDocument.Range.Paragraphs.Count
str = ActiveDocument.Range.Paragraphs(i).Range.Text
If isTrue(str, 1) > 0 And Val(str) > ktNo Then
ktNo = CInt(Val(str))
TotalKT = TotalKT + 1
End If
Next
ktNo = 0
MsgBox "共能找到答案" & TotalKT & "个。", vbInformation, "注意:"
If ActiveDocument.Endnotes.Count > 0 Then
If MsgBox("已有尾注" & ActiveDocument.Endnotes.Count & "个,需要全删除才能进行后续过程,删吗?", vbYesNo, "注意:") = vbNo Then
Exit Sub
End If
For m = 1 To ActiveDocument.Endnotes.Count
ActiveDocument.Endnotes.Item(1).Delete
Next
End If
Application.ScreenUpdating = True
For n = nextkeyStartp + 1 To ActiveDocument.Paragraphs.Count
If ActiveWindow.Selection.Tables.Count > 0 Then Exit For
ActiveDocument.Paragraphs(n).Range.Select
str = ActiveDocument.ActiveWindow.Selection.Text
If isTrue(str, 1) > 0 And Val(str) > ktNo Then
ktNo = CInt(Val(str))
keyStartp = ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Paragraphs.Count
ActiveWindow.Selection.Collapse Direction:=wdCollapseEnd
ActiveWindow.Selection.MoveLeft Unit:=wdCharacter, Count:=1
If ktNo = TotalKT Then
Set keyStartRange = ActiveDocument.Range(ActiveDocument.Paragraphs(keyStartp).Range.Start, ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range.End)
GoTo 341
End If
For c = keyStartp + 1 To ActiveDocument.Paragraphs.Count
ActiveDocument.Paragraphs(c).Range.Select
str = ActiveDocument.ActiveWindow.Selection.Text
If isTrue(str, 1) > 1 And Val(str) > ktNo Then
nextkeyStartp = ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Paragraphs.Count
Application.ScreenUpdating = True
Exit For
End If
If c = ActiveDocument.Paragraphs.Count Then
nextkeyStartp = ActiveDocument.Paragraphs.Count
End If
Next
Set keyStartRange = ActiveDocument.Range(ActiveDocument.Paragraphs(keyStartp).Range.Start, ActiveDocument.Paragraphs(nextkeyStartp - 1).Range.End)
341:
keyStartRange.Select
KeyTxt = ActiveWindow.Selection.Text
ActiveDocument.ActiveWindow.Selection.HomeKey Unit:=wdStory
With ActiveDocument.ActiveWindow.Selection.Find
.ClearFormatting
.Text = "^p" & ktNo & "."
.Forward = True
.Execute
If .Found = True Then
ActiveWindow.Selection.Collapse Direction:=wdCollapseEnd
If ActiveDocument.Range(0, Selection.Paragraphs(1).Range.End).Paragraphs.Count < keyStartp Then
ActiveDocument.Endnotes.Add ActiveWindow.Selection.Range, Text:=KeyTxt
End If
End If
End With
End If
Next
ActiveDocument.ActiveWindow.Selection.HomeKey Unit:=wdStory
ActiveDocument.Endnotes.NumberStyle = WdNoteNumberStyle.wdNoteNumberStyleArabic
Application.ScreenUpdating = True
End Sub
Public Function isTrue(ByVal sText As String, ByVal SelItem As Integer) As Integer
isTrue = 0
Dim reg
Set reg = CreateObject("vbscript.regexp")
Select Case SelItem
Case 1
With reg
.Global = True
.IgnoreCase = True
.Pattern = "\d{1,2}[.]"
If .test(sText) Then
isTrue = 1
End If
End With
Case 2
With reg
.Global = True
.IgnoreCase = True
.Pattern = "(^[【])([答])([案])([】])"
If .test(sText) Then
isTrue = 1
End If
End With
End Select
End Function
|
|