Private Sub Print_Click()
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim i, j As Integer
Dim WT As New clsWaitableTimer Set xlapp = CreateObject("excel.application")
Set xlbook = xlapp.Workbooks.Add
Set xlsheet = xlbook.Worksheets(1)
'On Error GoTo cls
xlsheet.Name = "数据记录"
xlsheet.Columns(1).ColumnWidth = 10
xlsheet.Columns(3).ColumnWidth = 6
xlsheet.Columns(4).ColumnWidth = 8
xlsheet.Columns(5).ColumnWidth = 8
xlsheet.Columns(9).ColumnWidth = 6
xlapp.Range("A1:I1").Merge
xlapp.Range("A1").Select
With Selection
.HorizontalAlignment = xlCenter
End With
With Selection.Font
.Size = 20
.Bold = True
End With
With xlsheet.PageSetup
.PrintTitleRows = "A2:I2"
End With
xlsheet.Cells(1, 1) = "数据报告"
For i = 0 To MSFlexGrid1.Rows - 1
For j = 0 To MSFlexGrid1.Cols - 1
xlsheet.Cells(i + 2, j + 1) = MSFlexGrid1.TextMatrix(i, j)
Next j
Next i
xlsheet.PageSetup.PrintGridlines = True
xlsheet.Cells.Borders.LineStyle = xlContinuous
xlapp.Visible = True
xlsheet.PrintPreview
xlapp.DisplayAlerts = False
xlbook.Close False
Set xlbook = Nothing
xlapp.Quit
Set xlapp = Nothing
End Sub
程序运行后,此段代码第一次执行没有错误,但再次被执行时报错,在.HorizontalAlignment = xlCenter这报错,实时错误'91'对象变量或With块变量未设置,那位大侠帮忙解决一下啊 感激不尽
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim i, j As Integer
Dim WT As New clsWaitableTimer Set xlapp = CreateObject("excel.application")
Set xlbook = xlapp.Workbooks.Add
Set xlsheet = xlbook.Worksheets(1)
'On Error GoTo cls
xlsheet.Name = "数据记录"
xlsheet.Columns(1).ColumnWidth = 10
xlsheet.Columns(3).ColumnWidth = 6
xlsheet.Columns(4).ColumnWidth = 8
xlsheet.Columns(5).ColumnWidth = 8
xlsheet.Columns(9).ColumnWidth = 6
xlapp.Range("A1:I1").Merge
xlapp.Range("A1").Select
With Selection
.HorizontalAlignment = xlCenter
End With
With Selection.Font
.Size = 20
.Bold = True
End With
With xlsheet.PageSetup
.PrintTitleRows = "A2:I2"
End With
xlsheet.Cells(1, 1) = "数据报告"
For i = 0 To MSFlexGrid1.Rows - 1
For j = 0 To MSFlexGrid1.Cols - 1
xlsheet.Cells(i + 2, j + 1) = MSFlexGrid1.TextMatrix(i, j)
Next j
Next i
xlsheet.PageSetup.PrintGridlines = True
xlsheet.Cells.Borders.LineStyle = xlContinuous
xlapp.Visible = True
xlsheet.PrintPreview
xlapp.DisplayAlerts = False
xlbook.Close False
Set xlbook = Nothing
xlapp.Quit
Set xlapp = Nothing
End Sub
程序运行后,此段代码第一次执行没有错误,但再次被执行时报错,在.HorizontalAlignment = xlCenter这报错,实时错误'91'对象变量或With块变量未设置,那位大侠帮忙解决一下啊 感激不尽
解决方案 »
- 我想让子窗口FORM1始终在FORM2之上,为什么这样没有用啊(注意是子窗口,不是普通窗口)?
- 怎样对一个1-3MB的文本文件进行查找替换操作.
- vb6 -〉文件 -〉生成工程1.exe 变成灰色了!怎么回事!
- VB的右键菜单 急!!!!!!!!
- 关于统计打印页数的问题
- 关于抛硬币问题
- 如何获取树节点的坐标位置?
- 发现ImageList的重大Bug,会迅速耗尽内存!!!
- 请问:"webbrowser的progid是什么?还有怎样可以知道一个控件的progid,或怎样设置?"谢谢!
- 在VB和VBA中编辑组合框是如何实现的?
- spread控件的问题:设置了行选中后如何防止双击选中单元格?
- vb通过ado command对象执行存贮过程,如何又能从参数返回,又能返回记录集呢?
Dim xlapp As Excel.Application
Dim xlbook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Dim i, j As Integer
Dim WT As New clsWaitableTimer Set xlapp = CreateObject("excel.application")
Set xlbook = xlapp.Workbooks.Add
Set xlsheet = xlbook.Worksheets(1)
'On Error GoTo cls
xlsheet.Name = "数据记录"
xlsheet.Columns(1).ColumnWidth = 10
xlsheet.Columns(3).ColumnWidth = 6
xlsheet.Columns(4).ColumnWidth = 8
xlsheet.Columns(5).ColumnWidth = 8
xlsheet.Columns(9).ColumnWidth = 6
xlapp.Range("A1:I1").Merge
xlapp.Range("A1").Select
xlapp.Cells.HorizontalAlignment = xlCenter
xlapp.Selection.Font.Size = 20
xlapp.Selection.Font.Bold = True
xlsheet.PageSetup.PrintTitleRows = "A2:I2" xlsheet.Cells(1, 1) = "数据报告"
For i = 0 To MSFlexGrid1.Rows - 1
For j = 0 To MSFlexGrid1.Cols - 1
xlsheet.Cells(i + 2, j + 1) = MSFlexGrid1.TextMatrix(i, j)
Next j
Next i
xlsheet.PageSetup.PrintGridlines = True
xlsheet.Cells.Borders.LineStyle = xlContinuous
xlapp.Visible = True
xlsheet.PrintPreview
xlapp.DisplayAlerts = False
xlbook.Close False
Set xlbook = Nothing
xlapp.Quit
Set xlapp = Nothing
End Sub