|
闲来没事,找些旧贴练练手
数据量较大,运行时要耐心等待
Sub lx()
Dim ar, wdapp As Object, mwd As Object, nwd As Object, i%, j%, k%
Application.ScreenUpdating = False
ar = Sheet1.UsedRange
Set wdapp = CreateObject("word.application")
wdapp.Visible = True
Set mwd = wdapp.documents.Open(ThisWorkbook.Path & "\word模板.doc")
mwd.Range.Copy
On Error Resume Next
If Dir(ThisWorkbook.Path & "\拆分文件\") = "" Then MkDir (ThisWorkbook.Path & "\拆分文件")
For i = 2 To UBound(ar)
If InStr(ar(i, 2), Chr(10)) > 0 Then ar(i, 2) = Replace(ar(i, 2), Chr(10), "")
If InStr(ar(i, 2), " ") > 0 Then ar(i, 2) = Replace(ar(i, 2), " ", "")
Set nwd = wdapp.documents.Add
nwd.Range.Paste
With nwd
.Paragraphs(2).Range.Text = Replace(.Paragraphs(2).Range.Text, "AA", ar(i, 2))
.tables(1).Range.Cells(2).Range.Text = ar(i, 3)
.tables(2).Range.Cells(2).Range.Text = ar(i, 6)
.tables(2).Range.Cells(4).Range.Text = ar(i, 5)
.tables(2).Range.Cells(6).Range.Text = ar(i, 4)
End With
nwd.SaveAs (ThisWorkbook.Path & "\拆分文件\" & ar(i, 1) & "-" & ar(i, 2) & ".docx")
nwd.Close
Next i
mwd.Close 0
Application.ScreenUpdating = True
End Sub
|
评分
-
1
查看全部评分
-
|