|
- Sub test()
- Dim r%, i%
- Dim arr, brr
- Dim wordapp As Object
- Dim worddoc As Object
- Dim mypath$, myname$
- Dim flg As Boolean
- Dim d As Object
- Set d = CreateObject("scripting.dictionary")
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- mypath = ThisWorkbook.Path & ""
- myname = "word文件.docx"
- If Dir(mypath & myname) = "" Then
- MsgBox mypath & myname & "不存在!"
- Exit Sub
- End If
- With Worksheets("sheet1")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- If r = 1 Then
- Exit Sub
- End If
- arr = .Range("a2:b" & r)
- For i = 1 To UBound(arr)
- d(arr(i, 1)) = arr(i, 2)
- Next
- End With
- On Error Resume Next
- Set wordapp = GetObject(, "word.application")
- If Err Then
- flg = True
- Set wordapp = CreateObject("word.application")
- End If
- On Error GoTo 0
- Set worddoc = wordapp.documents.Open(mypath & myname)
- With worddoc
- For i = 1 To .tables.Count
- With .tables(i)
- xm = Replace(.cell(5, 2).Range.Text, Chr(13) & Chr(7), Empty)
- If d.exists(xm) Then
- .cell(5, 4).Range.Text = d(xm)
- End If
- End With
- Next
- .Close True
- End With
- If flg Then
- wordapp.Quit
- End If
- Application.ScreenUpdating = True
- MsgBox "数据提取完毕!"
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|