首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

vb打印excel的有关问题

2012-03-05 
vb打印excel的问题Dim xlApp As Excel.ApplicationDim xlBook As Excel.WorkbookDim xlSheet As Excel.Wor

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

热点排行