在利用excel作为后台显示工具打印报表时,有如下代码:private sub cmdprint_click()
dim xlapp as  excel.application
dim xlbook as excel.workbook
dim xlsheet as excel.worksheet
set xlapp=createobject("excel.application")
set xlbook=xlapp.workbooks.add
set xlsheet=xlbook.worksheets(1)
xlsheet.cells(3,3)="打印"xlapp.displayalerts=false  /不出现另存为对话框
xlsheet.printout           /开始打印
xlapp.quit
set xlapp=nothing
end sub我没有连接打印机,可是点击按钮后,当程序执行到xlsheet.printout时,出现另外的“另存为”对话框,且保存类型为mdi,怎么才能避免出现此类对话框?

解决方案 »

  1.   

        Dim xlapp As excel.Application
        Set xlapp = New excel.Application
        xlapp.Visible = False
        xlapp.SheetsInNewWorkbook = 1
        Set xlbook = xlapp.Workbooks.Open(App.Path & "\excel\reports.xls")
        With xlapp.ActiveSheet
        .Cells(4, 1).Value =rs.Fields("卡号")
        .Cells(4, 2).Value = rs.Fields("姓名")
        .Cells(4, 3).Value = rs.Fields("水量")
        .Cells(4, 4).Value = Adodc1.Recordset.Fields("金额")
        .Cells(4, 5).Value = Mid(Adodc1.Recordset.Fields("日期"), 1, 10)
        .Cells(4, 6).Value = Adodc1.Recordset.Fields("用水性质")
        End With
        xlapp.ActiveSheet.PrintOut
        xlapp.DisplayAlerts = False
        xlapp.Quit
        xlapp.DisplayAlerts = True
        Set xlapp = Nothing
      

  2.   

    2楼的代码可能还会跳出保存提示框
    建议加  xlapp.ActiveSheet.Save 进行自动保存