|
可以试试如下代码,忽略保存位置的判断与添加等问题:
Sub GetInfo()
Dim myArray As Variant, i As Byte, a As String, info As String
Dim AppExcel As Object, WkBook As Object, r As Long
myArray = Array(10, 14, 16, 2, 24, 30) '主要单元格的索引号
With ActiveDocument
With .Content.Find '查找并提取发文日期
.Text = "^13????年[一-龥]@月[一-龥]@日^13"
.MatchWildcards = True
If .Execute Then
If .Parent.Paragraphs(2).Alignment = wdAlignParagraphRight Then
info = Replace(.Parent, Chr(13), "")
If IsDate(info) = False Then info = Replace(.Parent, "〇", "○")
info = Format(info, "General Date")
Else
MsgBox "没有找到右缩进的发文日期!"
Exit Sub
End If
Else
MsgBox "没有找到合适的发文日期!"
Exit Sub
End If
End With
With .Tables(1).Range '提取表格中的主要项目
For i = 0 To UBound(myArray)
a = Replace(.Cells(myArray(i)).Range, Chr(13) & Chr(7), "")
a = Trim(a)
If a = "" Then
MsgBox "表格中的主要项目有漏填!"
Exit Sub
Else
info = info & "|" & a
End If
Next
End With
End With
'数据写入excel文档
Set AppExcel = CreateObject("Excel.Application")
AppExcel.Visible = True
Set WkBook = AppExcel.Workbooks.Open(ActiveDocument.Path & "\发文登记簿(模板).xls") '暂设excel文档与发文稿保存在同一目录下
WkBook.Sheets(1).Activate
With WkBook.ActiveSheet
r = .Range("b2:b65536").End(-4121).Row + 1
.Cells(r, 1).Formula = "=row() - 2"
For i = 2 To 8
.Cells(r, i).Value = Split(info, "|")(i - 2)
Next
.Range(.Cells(r, 1), .Cells(r, 8)).Borders.LineStyle = 1
End With
WkBook.Close True
AppExcel.Quit
Set AppExcel = Nothing
MsgBox "数据已成功提取!"
End Sub |
|