office交流网--QQ交流群号

Access培训群:792054000         Excel免费交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

Excel 读取Word中指定的内容写到Excel单元格中

2020-04-22 08:00:00
zstmtony
原创
5880

Excel 读取Word中指定的内容写到Excel单元格中


 
Sub 抓取数据_Click()
  Dim wordApp As Object, docWord As Object
  Dim strPath As String
  Dim i As Long
strPath = ThisWorkbook.Path & "\提取模板.docx"
Set wordApp = CreateObject("Word.Application")
wordApp.Visible = True
Set docWord = wordApp.Documents.Open(strPath) '打开这个Word文件!
i = i + 1
GetData docWord, i
docWord.Close ' 关闭文件
Set wordApp = Nothing

End Sub


Public Function GetData(doc As Object, lngRow As Long)
Dim strText As String
Dim strStart As String
Dim strEnd As String
strStart = "授权证号-"
strEnd = "号"
With doc.Content.Find
    .Text = "(" & strStart & ")(*)(" & strEnd & ")"
    .MatchWildcards = True
   Do While .Execute
      strText = Replace(Replace(.Parent, strStart, ""), strEnd, "")
      ActiveSheet.Cells(lngRow, 1).Value = strText
   Loop
End With


strStart = "组织"
strEnd = "第1届"
With doc.Content.Find
    .Text = "(" & strStart & ")(*)(" & strEnd & ")"
    .MatchWildcards = True
   Do While .Execute
      strText = Replace(Replace(.Parent, strStart, ""), strEnd, "")
      ActiveSheet.Cells(lngRow, 2).Value = strText
   Loop
End With

End Function


    分享