vb打印excel的问题
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Private Sub Command1_Click()
Dim I As Integer
Set xlsApp = Excel.Application
xlsApp.Visible = False
Set xlsBook = xlsApp.Workbooks.Open("D:\sjk.xls")
xlsApp.Sheets(1).Cells(2, 3) = Text1.Text
xlsApp.Sheets(1).Cells(2, 5) = Combo1.Text
xlsApp.Sheets(1).Cells(3, 3) = Text2.Text
xlsBook.Close (True)
xlsApp.Quit
If Check1.Value = 1 Then
Set fso = CreateObject("scripting.filesystemobject")
fso.Copyfile "d:\sjk.xls", "d:\1\打印1.xls"
End If
If Check2.Value = 1 Then
Set fso = CreateObject("scripting.filesystemobject")
fso.Copyfile "d:\sjk.xls", "d:\1\打印2.xls"
End If
End Sub
Private Sub Command3_Click()
Set ExcelxlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set ExcelxlBook = ExcelxlApp.Workbooks.Open("d:\1\打印1.xls") '打开已存在的ExcEL 工作薄文件
ExcelxlApp.Visible = True '设置EXCELS对象可见(或不可见)
Set ExcelxlSheet = ExcelxlBook.Worksheets(sheet1) '设置活动工作表
ExcelxlSheet.PrintOut '打印
End Sub
Private Sub Command4_Click()
End
End Sub
Command1是将输入内容保存至excel,并根据条件复制,我想用Command3打印Command1中刚复制的文件,但我上面的代码不行,请问如何Command3中的打印代码该如修改?
谢谢!
[解决办法]
Set ExcelxlSheet = ExcelxlBook.Worksheets("sheet1")
[解决办法]
Private Sub Command3_Click()
Dim ExcelxlApp, ExcelxlBook, ExcelxlSheet ' 这变量最好定义一下
Set ExcelxlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set ExcelxlBook = ExcelxlApp.Workbooks.Open("c:\1.xls") '打开已存在的ExcEL 工作薄文件
ExcelxlApp.Visible = True '设置EXCELS对象可见(或不可见)
Set ExcelxlSheet = ExcelxlBook.Worksheets("sheet1") '问题出在这里,sheet1加双引 ExcelxlSheet.PrintOut '打印
End Sub