Sub 提取Word文档内容到Excle()
On Error GoTo Err_cmdimportWord_Click
Dim objwd As Object
Dim objdoc As Object
Dim objTable As Object
Dim strTemplates As String 'word文件路径名
Dim k, n As Integer
Dim mypath As String, myname As String
Dim i As Long
Set objwd = CreateObject("Word.Application") ' 建立Word会话
objwd.Visible = False ' 设定Word应用程序为不可见状态
k = Cells.Find("*", LookIn:=xlFormulas, SearchDirection:=xlPrevious).Row + 1 '查找最后一行行号
n = 0
Application.ScreenUpdating = False
With Application.FileDialog(msoFileDialogFilePicker) '选择单个word文件
.Filters.Add "word文件", "*.doc*", 1
.AllowMultiSelect = False
If .Show Then strTemplates = .SelectedItems(1) Else Exit Sub
End With
Set objdoc = objwd.Documents.Open(strTemplates, , False)
Set objTable = objdoc.Tables(1)
mypath = strTemplates
myname = Dir(mypath & "*.*")
Sheet1.Cells(n + k, 2) = myname
Sheet1.Cells(n + k, 3) = Replace(objTable.Cell(1, 2).Range.Text, Chr$(13) & Chr$(7), "")
Sheet1.Cells(n + k, 4) = Replace(objTable.Cell(1, 4).Range.Text, Chr$(13) & Chr$(7), "")
Sheet1.Cells(n + k, 5) = Replace(objTable.Cell(1, 6).Range.Text, Chr$(13) & Chr$(7), "")
Sheet1.Cells(n + k, 6) = Replace(objTable.Cell(2, 2).Range.Text, Chr$(13) & Chr$(7), "")
Sheet1.Cells(n + k, 7) = Replace(objTable.Cell(2, 4).Range.Text, Chr$(13) & Chr$(7), "")
Sheet1.Cells(n + k, 8) = Replace(objTable.Cell(2, 6).Range.Text, Chr$(13) & Chr$(7), "")
Sheet1.Cells(n + k, 9) = Replace(objTable.Cell(3, 2).Range.Text, Chr$(13) & Chr$(7), "")
Sheet1.Cells(n + k, 10) = Replace(objTable.Cell(3, 4).Range.Text, Chr$(13) & Chr$(7), "")
Sheet1.Cells(n + k, 11) = Replace(objTable.Cell(4, 2).Range.Text, Chr$(13) & Chr$(7), "")
Sheet1.Cells(n + k, 12) = Replace(objTable.Cell(5, 2).Range.Text, Chr$(13) & Chr$(7), "")
Sheet1.Cells(n + k, 13) = Replace(objTable.Cell(6, 2).Range.Text, Chr$(13) & Chr$(7), "")
Sheet1.Cells(n + k, 14) = Replace(objTable.Cell(7, 2).Range.Text, Chr$(13) & Chr$(7), "")
objdoc.Close ' 关闭文件
Application.ScreenUpdating = True
Exit_cmdimportWord_Click:
Set objdoc = Nothing
Set objTable = Nothing
Set objwd = Nothing
Exit Sub
Err_cmdimportWord_Click:
MsgBox Err.Description, vbCritical, "出错"
Resume Exit_cmdimportWord_Click
End Sub