|
- Sub test()
- Dim r%, i%
- Dim arr, brr
- Dim d As Object
- Dim wordapp As Object
- Dim mydoc As Object
- Set d = CreateObject("scripting.dictionary")
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- With Worksheets("sheet1")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- arr = .Range("a2:f" & r)
- End With
- For i = 1 To UBound(arr)
- If Not d.exists(arr(i, 4)) Then
- Set d(arr(i, 4)) = CreateObject("scripting.dictionary")
- End If
- If Not d(arr(i, 4)).exists(arr(i, 6)) Then
- Set d(arr(i, 4))(arr(i, 6)) = CreateObject("scripting.dictionary")
- End If
- d(arr(i, 4))(arr(i, 6))(arr(i, 5)) = arr(i, 2)
- Next
- On Error Resume Next
- Set wordapp = GetObject(, "word.application")
- If Err Then
- Set wordapp = CreateObject("word.application")
- End If
- On Error GoTo 0
- For Each aa In d.keys
- FileCopy ThisWorkbook.Path & "\评分表模版.docx", ThisWorkbook.Path & "\结果" & aa & "评分表.docx"
- Set mydoc = wordapp.documents.Open(Filename:=ThisWorkbook.Path & "\结果" & aa & "评分表.docx")
- With mydoc
- With wordapp.Selection.Find
- .ClearFormatting
- .Text = "班级:"
- .Replacement.ClearFormatting
- .Replacement.Text = "班级:" & aa
- .Execute Replace:=wdReplaceAll
- End With
- For Each tbl In .tables
- tbl.Select
- With tbl
- Do
- wordapp.Selection.MoveUp wdLine, 1, wdMove
- wordapp.Selection.EndKey wdLine, wdExtend
- Loop Until wordapp.Selection.Range.Text Like "实验*"
- synr = Left(wordapp.Selection.Range.Text, 3)
- If d(aa).exists(synr) Then
- For j = 4 To 10
- ss = Val(Replace(.Cell(2, j).Range.Text, Chr(13) & Chr(7), Empty))
- If d(aa)(synr).exists(ss) Then
- .Cell(3, j).Range.Text = d(aa)(synr)(ss)
- End If
- Next
- End If
- End With
- Next
- .Close True
- End With
- Next
- If Err Then
- wordapp.Quit
- End If
- Application.ScreenUpdating = True
- MsgBox "生成完毕!"
- End Sub
复制代码 |
|