Sub 填充人口信息采集()
Dim wsSource As Worksheet
Dim docTarget As Object ' Word文档对象
Dim appWord As Object ' Word应用程序对象
Dim docTemplatePath As String
Dim lastRow As Long, i As Long
Dim householdStart As Long, householdEnd As Long
Dim fileCount As Long
Dim outputFolder As String
' 启动Word应用程序
On Error Resume Next
Set appWord = GetObject(, "Word.Application")
If Err.Number 0 Then
Set appWord = CreateObject("Word.Application")
End If
On Error GoTo 0
appWord.Visible = False
' 初始化计数器
fileCount = 0
householdStart = 0
' 遍历所有行,查找户主
For i = 2 To lastRow
If Trim(wsSource.Cells(i, 13).Value) = "户主" Then
' 如果找到前一个户主的结束位置
If householdStart > 0 Then
householdEnd = i - 1
Call 填充单个家庭(wsSource, appWord, docTemplatePath, householdStart, householdEnd, outputFolder)
fileCount = fileCount + 1
End If
householdStart = i
End If
Next i
' 处理最后一个家庭
If householdStart > 0 Then
householdEnd = lastRow
Call 填充单个家庭(wsSource, appWord, docTemplatePath, householdStart, householdEnd, outputFolder)
fileCount = fileCount + 1
End If
Sub 填充单个家庭(wsSource As Worksheet, appWord As Object, docTemplatePath As String, startRow As Long, endRow As Long, outputFolder As String)
Dim docTarget As Object
Dim table As Object
Dim householdName As String, fileName As String, targetPath As String
Dim i As Long, rowIndex As Long
Dim birthDate As Date
Dim familyMemberCount As Long
On Error GoTo ErrorHandler
' 获取户主姓名(第2列)
householdName = Trim(wsSource.Cells(startRow, 2).Value)
If householdName = "" Then
Exit Sub ' 如果户主姓名为空,跳过
End If