|
本帖最后由 fxl447098457 于 2017-11-30 07:12 编辑
word vba.表格填充了部分,供参考。
- Sub dataToWord()
- Dim xlApp As Object, k As Long, Connect$, StrSQL$, myname$
- Dim d As Object, Arr, i%, ds, Rst As Object, m%
- Set d = VBA.CreateObject("scripting.dictionary")
- Set Rst = VBA.CreateObject("adodb.recordset")
- Set xlApp = VBA.CreateObject("Excel.Application")
- With xlApp.Workbooks.Open(ActiveDocument.Path & "\excel数据.xlsx").sheets(1)
- Arr = .Range("A2:N" & .Cells(.Rows.Count, 1).End(3).Row)
- .Parent.Close 0
- End With
- Rst.cursorlocation = 3
- Connect = "Provider=Microsoft.ACE.oledb.12.0;extended properties=excel 12.0;data source=" _
- & ActiveDocument.Path & "\excel数据.xlsx"
- For i = 1 To UBound(Arr)
- d(Arr(i, 1)) = ""
- Next
- k = 2
- Application.ScreenUpdating = False
- For Each ds In d.KEYS
- StrSQL = "Select * from [Sheet1$A:N] Where 授课老师='" & ds & "'"
- Rst.Open StrSQL, Connect, 1, 3
- If Rst.RecordCount Then
- With ActiveDocument
- With .Content.Find
- m = m + 1
- .Execute findtext:=IIf(m = 1, " 老师", myname & "老师"), Replacewith:=ds & "老师", Replace:=wdReplaceOne
- End With
- With .Tables(1)
- Do While Not Rst.EOF
- k = k + 1
- .Cell(k, 1).Range.Text = Rst.Fields(2).Value
- .Cell(k, 2).Range.Text = Rst.Fields(1).Value
- .Cell(k, 3).Range.Text = Rst.Fields(6).Value
- .Cell(k, 4).Range.Text = Rst.Fields(7).Value
- .Cell(k, 5).Range.Text = IIf(Rst.Fields(4).Value = Split(.Cell(1, 5).Range.Text, vbCr)(0), "Y", "")
- .Cell(k, 6).Range.Text = IIf(Len(.Cell(1, 5).Range.Text) > 1, "", "Y")
- Rst.MoveNext
- Loop
- End With
- k = 2
- .SaveAs2 ActiveDocument.Path & "" & ds & ".docx", wdFormatXMLDocument
- End With
- End If
- Rst.Close
- myname = ds
- Next ds
- Application.ScreenUpdating = True
- Set xlApp = Nothing
- Set d = Nothing
- Set Rst = Nothing
- End Sub
复制代码
|
|