|
楼主 |
发表于 2015-10-26 14:57
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
经多方求助,得出的结论是无法用邮件合并打印多行记录。看来只能用VBA- Sub 批量打印()Set ADOConn = CreateObject("ADODB.Connection")
- Set ADORst = CreateObject("ADODB.Recordset")
- conn = "provider=microsoft.ACE.OLEDB.12.0;extended properties='Excel 12.0;hdr=yes';data source=" & ThisWorkbook.FullName
- Rmax = Sheets("查询清单").Range("a1").CurrentRegion.Rows.Count
- For i = 2 To Rmax
- SVoucher = Sheets("查询清单").Cells(i, 1)
- SProject = Sheets("查询清单").Cells(i, 2)
- Sheets("查询结果").Cells.Clear
- SQL = "select 名称,规格,单位,数量,单价,金额 from [项目名称$] where [凭证号]=""" & SVoucher & """ And [项目名称]= """ & SProject & """" '注意在VBA中SQL使用where多条件查询时的表达方法
- ADOConn.Open conn
- ADORst.Open SQL, ADOConn, 3, 3
- Debug.Print ADORst.RecordCount
- For k = 0 To ADORst.Fields.Count - 1
- Sheets("查询结果").Cells(1, k + 1) = ADORst.Fields(k).name
- Next k
- Sheets("查询结果").[a2].CopyFromRecordset ADORst
- With Sheets("打印模板")
- Union(.[B2], .[C2], .[G2]).ClearContents
- .[B2] = SProject
- .[e2] = Right(SVoucher, Len(SVoucher) - InStrRev(SVoucher, "-"))
- .[G2] = Left(SVoucher, InStrRev(SVoucher, "-")) & "28"
- ' ' For X = 2 To Sheets("查询结果").Cells(Rows.Count, 1).End(xlUp).Row Step 10
- For X = 2 To ADORst.RecordCount + 1 Step 10
- Sheets("打印模板").Range("b4:g13").ClearContents
- Sheets("查询结果").Range("A" & X & ":F" & X + 9).Copy Sheets("打印模板").Cells(4, 2)
- ActiveSheet.PrintOut From:=1, To:=1, Copies:=1
- Next
- End With
- ADORst.Close
- ADOConn.Close
- Next i
- Set ADORst = Nothing
- Set ADOConn = Nothing
- End Sub
复制代码
|
|