|
Option Explicit
Sub test()
Dim wdApp As Object, strFileName$, strPath$
Dim ar, i&, j&, k&, p&, m&, n&, strSaveName$
strPath = ThisWorkbook.Path & "\"
strFileName = strPath & "登记表.docx"
If Dir(strFileName) = "" Then MsgBox "模板文件不存在,请检查!", vbExclamation: Exit Sub
Application.ScreenUpdating = False
With [A1].CurrentRegion
ar = .Resize(.Rows.Count + 1).Value
End With
On Error Resume Next
Set wdApp = GetObject(, "Word.Application")
If Err <> 0 Then
Set wdApp = CreateObject("Word.Application")
End If
p = 2
For i = 2 To UBound(ar) - 1
If ar(i + 1, 1) <> ar(p, 1) Then
With wdApp.documents.Open(strFileName)
strSaveName = strPath & ar(p, 1)
m = 3: n = i - p + 1
With .Tables(1)
For k = p To i
m = m + 1
For j = 1 To UBound(ar, 2)
.Cell(m, j).Range.Text = ar(k, j)
Next j
Next k
End With
With .Content.Find
.ClearFormatting
.Execute FindText:="户号:", Forward:=True
If .Found = True Then .Parent.Text = "户号:" & ar(p, 1)
End With
With .Content.Find
.ClearFormatting
.Execute FindText:="家庭总人口数:共", Forward:=True
If .Found = True Then
With wdApp.ActiveDocument.Range(.Parent.End + 2, .Parent.End + 2)
.InsertAfter n
End With
End If
End With
.SaveAs strSaveName: .Close
End With
p = i + 1
End If
Next i
If Err <> 0 Then wdApp.Quit
Set wdApp = Nothing
Application.ScreenUpdating = True
Beep
End Sub
|
评分
-
1
查看全部评分
-
|