喝水不忘挖井人,之前提问官方没有给出答案,不过帮助里有,搜索论坛发现也有不少和我一样的菜鸟有这样的问题,贴出来分享下代码
'==========打印机遍历代码,我放在窗口的afterload事件
Dim cmb As WinForm.ComboBox = e.form.Controls("打印机选择")
For Each p As String In InstalledPrinters
cmb.Items.Add(p)
Next
cmb.Text = GetConfigValue("pntname1","请选择打印机")
'=================================打印按钮代码
Dim pnt As WinForm.ComboBox = e.Form.Controls("打印机选择")'取得打印机名称
Dim pntn As String = pnt.Text
SaveConfigValue("pntname1",pntn)
Dim Book As New XLS.Book(ProjectPath & "Attachments\表格名.xls")
Dim fl As String = ProjectPath & "Reports\表格名.xls"
Book.Build() '生成细节区
Book.Save(fl) '保存工作簿
Dim App As New MSExcel.Application
Dim Wb As MSExcel.WorkBook = App.WorkBooks.Open(fl)
Dim Ws As MSExcel.WorkSheet = Wb.WorkSheets(1)
Ws.PrintOut(ActivePrinter:=pntn)
App.quit '这个一定要写,退出进程里的EXCEL,否则会提示XXX已经打开
帮助里有写
Ws.PrintOut(From:=1,To:=10,Copies:=3,Preview:=True,ActivePrinter:="pdfFactory Pro")
打印第1-10页,打印份数为3,打印前先预览,指定的打印机为"pdfFactory Pro"