|
楼主 |
发表于 2018-3-21 14:15
|
显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
目前的任务是这样的:(1)在excel中,根据数据有效性,分别筛选每个单位的相关数据,表现在同一个表格区域内,比如A1:G20。(2)word文件内有这些单位分别对应的提示函内容,每个单位为一页或一节,具体内容由邮件合并产生。(3)最后一步,每次筛选一个单位,就复制A1:G20的表格粘贴到这个单位在word中对应页的第7段。现在只有第3步无法实现。目前我的代码是这样的:
Sub 生成()
Dim dpath, Filename As String
Dim wdapp As Object, wrddoc
Dim ws As Worksheet, arr
Dim rng As Range
Dim s As Section
On Error Resume Next
Set ws = Worksheets("选择")
Set wdapp = CreateObject("Word.Application")
wdapp.Visible = 0
Application.ScreenUpdating = False
arr = ws.Range("B31:B" & ws.Range("B65536").End(xlUp).Row)
dpath = ThisWorkbook.Path
Filename = Dir(dpath & "\正面.doc")
Set wrddoc = wdapp.Documents.Open(dpath & "\" & Filename) '打开word
i = 1
With ActiveDocument '调测
For Each s In .Sections
If s.Range.Paragraphs.Count = 1 Then Exit For
If s.Range.Paragraphs.Count > 6 Then
Do While i <= UBound(arr)
Excel.Application.Sheets("选择").Activate
[K4] = arr(i, 1)
Sheets("处理").Columns("A:G").AdvancedFilter Action:=xlFilterCopy, _
CriteriaRange:=Range("K3:K4"), CopyToRange:=Range("A2:G20"), Unique:= _
False
ActiveWindow.SmallScroll Down:=3
Range("B3:G20").Select
ActiveWorkbook.Worksheets("选择").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("选择").Sort.SortFields.Add Key:=Range( _
"B3:B20"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("选择").Sort
.SetRange Range("B3:G20")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets("选择").Range("B1:G" & Sheets("选择").Range("G65536").End(xlUp).Row).Copy
wdapp.Application.Activate
s.Range.Paragraphs(7).Range.Paste
i = i + 1
Exit Do
Next
End With
wdapp.Application.Activate '保存word文件
wdapp.Saved = True
wdapp.ActiveDocument.SaveAs "D:\使用文件.doc"
wdapp.Application.Quit
Set wdapp = Nothing
Application.ScreenUpdating = True
End Sub
|
|