简体   繁体   English

Access 2013:将数据复制到Excel并打印(VBA)

[英]Access 2013: copy data to Excel and print (VBA)

I'd like a button that, when clicked, copies data from "field1" and "field2" into an Excel spreadsheet to "field 3" and "field 4" and prints the outcome. 我想要一个按钮,单击该按钮时,会将数据从“ field1”和“ field2”复制到Excel电子表格中,并复制到“ field 3”和“ field 4”并打印结果。

Basically, once someone places an order and I click the "Paid" button, it pushes the info into a template and prints. 基本上,一旦有人下订单,然后单击“已付款”按钮,它将信息推送到模板中并进行打印。

The following code should get you started. 以下代码将帮助您入门。 The Excel template OrderSheet.xltx has two single-cell Named Ranges: field3 and field4 . Excel模板OrderSheet.xltx具有两个单单元格命名范围: field3field4 The rest should be pretty self-explanatory. 其余的应该是不言自明的。

Private Sub cmdPlaceOrder_Click()
Dim objExcel As Object  '' Excel.Application
Dim objSheet As Object  '' Excel.Worksheet
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Add "C:\Users\Public\OrderSheet.xltx"
objExcel.Range("field3").Value = Me.txtField1.Value
objExcel.Range("field4").Value = Me.txtField2.Value
Set objSheet = objExcel.ActiveSheet
objSheet.PrintOut
Set objSheet = Nothing
objExcel.ActiveWorkbook.Close False  '' save changes = No
objExcel.Quit
Set objExcel = Nothing
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM