|
楼主 |
发表于 2023-12-30 19:54
|
显示全部楼层
老师,多谢你编写的代码,能按我的要求添加对应的数据,但一直循环的加入,不停止。在你的代码基础上我修改了一下,顺利的写入完成。
Sub lqxs()
Dim WD As Object, DC As Object
Dim tb As Object, bm$, nm$, myPath$, myName$
Dim Arr, i%, d, n%, s4$
Set d = CreateObject("Scripting.Dictionary")
Set WD = CreateObject("Word.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Sheet1.Activate
Arr = Range("A1").CurrentRegion
For i = 2 To UBound(Arr)
bm = Arr(i, 2) & "_" & Arr(i, 3) & "_登记簿"
d(bm) = i
Next
myPath = ThisWorkbook.Path & "\"
Set objFolder = objFSO.GetFolder(myPath)
For Each objFile In objFolder.Files
If LCase(Right(objFile.Name, 4)) = ".doc" Then
nm = Split(objFile.Name, ".")(0)
If d.exists(nm) Then
n = d(nm)
Set DC = WD.documents.Open(myPath & objFile.Name)
WD.Visible = True
Set tb = DC.tables(1)
With tb
.Range.Cells(22).Range.Text = Arr(n, 4)
s4 = .Range.Cells(28).Range.Text
.Range.Cells(28).Range.Text = s4 & Arr(n, 5)
End With
DC.Close True
End If
End If
Next objFile
Set WD = Nothing
End Sub
|
|