|
本帖最后由 守柔 于 2015-6-8 07:46 编辑
感于413191246se兄的热情.
我随手写了一个,请413191246se兄和楼主测试一下.
特别说明:
请楼主千万不要问我如何运行宏代码,抱歉.
Private Sub OutputExaminees()
Dim oShape As Word.Shape
Dim myRange As Word.Range
Dim astrExaminees() As String
Dim astrSpliter() As String
Dim strText As String
Dim I As Integer
Dim intPageNum As Integer
I = 1 ''''初始化变量
ReDim astrExaminees(1 To 3, 1 To I) ''''动态定义一个数组,存储考生信息
astrExaminees(1, I) = "全国学籍号"
astrExaminees(2, I) = "姓名"
astrExaminees(3, I) = "注册码"
For Each oShape In ActiveDocument.Shapes ''''遍历活动文档的图形
With oShape
If .Type = msoAutoShape Then ''''为自选图形
If .AutoShapeType = msoShapeRectangle Then ''''为矩形
If .TextFrame.HasText Then ''''具有文本内容时
strText = Replace$(.TextFrame.TextRange.Text, Chr$(13), Empty) ''''删除段落标记
If strText Like String$(16, 35) Then ''''为16位数字,即"################"
I = I + 1 ''''计数
ReDim Preserve astrExaminees(1 To 3, 1 To I) ''''动态扩充数组
astrExaminees(3, I) = strText ''''注册码
intPageNum = .Anchor.Information(wdActiveEndPageNumber) ''''注册码所在矩形区域的页码
Set myRange = ActiveDocument.GoTo(What:=wdGoToPage, Which:=wdGoToAbsolute, Name:=CStr(intPageNum)) ''''定义到当前页
strText = Replace$(myRange.Paragraphs(1).Range.Text, Chr$(32), Empty, , , vbTextCompare) ''''当前页面下第一个段落所在文本(全国学籍号/姓名)
astrSpliter = Split(strText, ChrW$(-230), , vbTextCompare)
If UBound(astrSpliter) = 2 Then
astrExaminees(1, I) = Replace$(astrSpliter(1), "姓名", Empty) ''''Left$(astrSpliter(1), 14) ''''全国学籍号,14位长度,发现示例文件中的蒋梦丹两页上的全国学籍号不一致!
astrExaminees(2, I) = Replace$(astrSpliter(2), Chr$(13), Empty) ''''姓名,删除段落标记
End If
End If
End If
End If
End If
End With
Next
If I > 1 Then
ReDim astrSpliter(1 To I) ''''动态定义一个一维数组,存储输出段落
For I = 1 To UBound(astrExaminees, 2)
astrSpliter(I) = astrExaminees(1, I) & vbTab & astrExaminees(2, I) & vbTab & astrExaminees(3, I) ''''各字段之间使用TAB键分隔
Next
Application.Documents.Add.Content.InsertAfter Join(astrSpliter, Chr$(13)) ''''在新文档中输出提取结果
End If
End Sub
|
|