|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
Sub WordVBA()
Dim conn As Object, pf$, Sql$, arr, doc As Document, sr$
pf = ThisDocument.Path & "\库存数据.xlsx"
Sql = "select 名称,数量,单位 from [" & pf & "].[数据$a2:n] where 编号 is not null"
Set conn = CreateObject("adodb.connection")
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;extended properties=EXCEL 12.0;data source=" & pf
arr = conn.Execute(Sql).GetRows
For i = 0 To UBound(arr, 2)
sr = sr & "," & arr(0, i) & " " & arr(1, i) & arr(2, i)
Next
Set doc = Documents.Add(Visible:=fase)
With doc.Content
.InsertAfter "XX方剂" & vbCr & "日期:" & Date & vbCr & "成分:" & Mid(sr, 2)
End With
doc.SaveAs ThisDocument.Path & "\XX方剂1.docx"
doc.Close
conn.Close
Set conn = Nothing
End Sub |
|