|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub 生成word文档()
Application.ScreenUpdating = False
Dim oFso
Set oFso = CreateObject("Scripting.FileSystemObject")
Dim i%, arr, myPath$, wdApp, wdD
Dim ar As Variant, cr As Variant
Dim br(), brr()
Dim d As Object
Set d = CreateObject("scripting.dictionary")
myPath = ThisWorkbook.Path & "\"
Set sh = ThisWorkbook.Worksheets(1)
With Sheets("Sheet1")
r = .Cells(Rows.Count, 1).End(xlUp).Row
ar = .Range("a1:g" & r)
End With
For i = 2 To UBound(ar)
If Trim(ar(i, 1)) <> "" Then
d(Trim(ar(i, 1))) = ar(i, 2)
End If
Next i
Set wdApp = CreateObject("word.application")
wdApp.Visible = False
For Each k In d.keys
n = 0: je = 0
ReDim br(1 To UBound(ar), 1 To 4)
For i = 2 To UBound(ar)
If Trim(ar(i, 1)) = k Then
je = je + ar(i, 6)
dw = ar(i, 2) & "(" & ar(i, 1) & ")"
n = n + 1
For j = 3 To 6
br(n, j - 2) = ar(i, j)
Next j
If Trim(ar(i, 7)) <> "" Then bh = ar(i, 7)
End If
Next i
rq = br(1, 1) & "至" & br(n, 1)
sh.[l1] = je
dje = sh.[j1]
FileCopy myPath & "模板.doc", myPath & "文书\" & k & ".doc"
Set wdD = wdApp.Documents.Open(myPath & "文书\" & k & ".doc")
With wdApp
.Selection.HomeKey Unit:=6 'wdStory '光标置于文件首
If .Selection.Find.Execute("sj1") Then '查找到指定字符串
.Selection.Text = bh '替换字符串
End If
.Selection.HomeKey Unit:=6 ''wdStory '光标置于文件首
If .Selection.Find.Execute("sj2") Then '查找到指定字符串
.Selection.Text = dw '替换字符串
End If
.Selection.HomeKey Unit:=6 'wdStory '光标置于文件首
If .Selection.Find.Execute("sj3") Then '查找到指定字符串
.Selection.Text = rq '替换字符串
End If
.Selection.HomeKey Unit:=6 'wdStory '光标置于文件首
If .Selection.Find.Execute("sj4") Then '查找到指定字符串
.Selection.Text = dje '替换字符串
End If
.Selection.HomeKey Unit:=6 'wdStory '光标置于文件首
If .Selection.Find.Execute("sj5") Then '查找到指定字符串
.Selection.Text = je '替换字符串
End If
End With
wdD.Save
wdD.Close True
Next k
wdApp.Quit
Set wdD = Nothing
Set wdApp = Nothing
Application.ScreenUpdating = False
MsgBox "ok!"
End Sub
Sub 按模板生成文档()
Application.ScreenUpdating = False
Dim i%, arr, myPath$, wdApp, wdD
Dim oFso
Set oFso = CreateObject("Scripting.FileSystemObject")
Dim d As Object
Set d = CreateObject("scripting.dictionary")
myPath = ThisWorkbook.Path & "\"
Set wdApp = CreateObject("word.application")
wdApp.Visible = False
arr = Sheets("sheet1").Range("A1:m" & Cells(Rows.Count, 1).End(xlUp).Row)
For i = 3 To UBound(arr)
If Trim(arr(i, 1)) = "" Then
arr(i, 1) = arr(i - 1, 1)
arr(i, 2) = arr(i - 1, 2)
arr(i, 3) = arr(i - 1, 3)
End If
zf = Trim(arr(i, 1)) & "-" & Trim(arr(i, 3))
d(zf) = ""
Next i
For Each k In d.keys
n = 0: hz = ""
ReDim brr(1 To UBound(arr), 1 To 4)
For i = 3 To UBound(arr)
If Trim(arr(i, 1)) & "-" & Trim(arr(i, 3)) = k Then
zb = arr(i, 2)
n = n + 1
brr(n, 1) = arr(i, 6)
brr(n, 2) = arr(i, 7)
brr(n, 3) = arr(i, 9)
brr(n, 4) = Mid(Trim(arr(i, 8)), 7, 8)
End If
Next i
FileCopy myPath & "___村集体经济组织成员股权登记簿.docx", myPath & "生成的文件\" & "___村集体经济组织成员股权登记簿" & k & ".docx"
Set wdD = wdApp.Documents.Open(myPath & "生成的文件\" & "___村集体经济组织成员股权登记簿" & k & ".docx")
With wdD.Tables(1)
.cell(2, 2).Range.Text = Split(k, "-")(0)
.cell(2, 5).Range.Text = Split(k, "-")(1)
.cell(2, 8).Range.Text = zb
For i = 1 To n
For j = 1 To 4
.cell(i + 4, j).Range.Text = brr(i, j)
Next j
Next i
End With
wdD.Save
wdD.Close True
Next k
wdApp.Quit
Set wdD = Nothing
Set wdApp = Nothing
Application.ScreenUpdating = False
MsgBox "ok!"
End Sub
|
|