第一次导出可,第二次导出就出错。为什么?
Private Sub cmdToExcel_Click()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Open("c:\a.xls") '打开已经存在的EXCEL工件簿文件
xlApp.Visible = True '设置EXCEL对象可见(或不可见) Set xlsheet = xlBook.Worksheets(1) '设置活动工作表Set xlSheet = xlBook.Worksheets("sheet1") '
xlsheet.Activate '激活工作表?
xlsheet.Cells(2, 4) = Me.Caption '给单元格1行驶列赋值
xlsheet.Cells(3, 4) = DTPicker1.Value & "-" & DTPicker2.Value
Dim begrow As Integer '从第行开始表体
begrow = 3
With xlsheet
For R = 1 To MSG.Rows
For C = 1 To MSG.Cols
.Cells(R + begrow, C) = MSG.TextMatrix(R - 1, C - 1)
Next C
Next R
If R = 1 Then
Range("A" & begrow + 1 & ":" & "I" & begrow + R).Select
Else
Range("A" & begrow + 1 & ":" & "I" & begrow + R - 1).Select
End If
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
If R <> 1 Then
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End If
End With
Screen.MousePointer = vbDefault
'三.打印预览
xlApp.ActiveSheet.PageSetup.Orientation = xlPortrait ' 设置打印方向
xlApp.ActiveSheet.PageSetup.PaperSize = xlPaperA4 ' 设置打印纸的打下
xlApp.Caption = "打印预览" '设置预览窗口的 标题
xlApp.ActiveSheet.PrintPreview '打印预览
xlApp.ActiveSheet.PrintOut '打印输出 xlBook.Close (True) '关闭EXCEL工作簿
xlApp.Quit '关闭EXCEL
Set xlApp = Nothing '释放EXCEL对象
MsgBox "转出完成!"
End Sub
Private Sub cmdToExcel_Click()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlsheet As Excel.Worksheet
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Open("c:\a.xls") '打开已经存在的EXCEL工件簿文件
xlApp.Visible = True '设置EXCEL对象可见(或不可见) Set xlsheet = xlBook.Worksheets(1) '设置活动工作表Set xlSheet = xlBook.Worksheets("sheet1") '
xlsheet.Activate '激活工作表?
xlsheet.Cells(2, 4) = Me.Caption '给单元格1行驶列赋值
xlsheet.Cells(3, 4) = DTPicker1.Value & "-" & DTPicker2.Value
Dim begrow As Integer '从第行开始表体
begrow = 3
With xlsheet
For R = 1 To MSG.Rows
For C = 1 To MSG.Cols
.Cells(R + begrow, C) = MSG.TextMatrix(R - 1, C - 1)
Next C
Next R
If R = 1 Then
Range("A" & begrow + 1 & ":" & "I" & begrow + R).Select
Else
Range("A" & begrow + 1 & ":" & "I" & begrow + R - 1).Select
End If
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
.LineStyle = xlDouble
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeTop)
.LineStyle = xlDouble
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlEdgeRight)
.LineStyle = xlDouble
.Weight = xlThick
.ColorIndex = xlAutomatic
End With
With Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
If R <> 1 Then
With Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End If
End With
Screen.MousePointer = vbDefault
'三.打印预览
xlApp.ActiveSheet.PageSetup.Orientation = xlPortrait ' 设置打印方向
xlApp.ActiveSheet.PageSetup.PaperSize = xlPaperA4 ' 设置打印纸的打下
xlApp.Caption = "打印预览" '设置预览窗口的 标题
xlApp.ActiveSheet.PrintPreview '打印预览
xlApp.ActiveSheet.PrintOut '打印输出 xlBook.Close (True) '关闭EXCEL工作簿
xlApp.Quit '关闭EXCEL
Set xlApp = Nothing '释放EXCEL对象
MsgBox "转出完成!"
End Sub
解决方案 »
- VB6中如何让很多独立文本框中的某一个获得焦点时选中其全部内容
- 在一个Frame内安排几个CheckBox框(即一个CheckBox框数组),能否根据需要自动在窗体上增加CheckBox框的数量,不用预先在窗体上将几个Che
- 各位大师来救我一命.关于findwindow()的,救命啊!!!!!!!
- vb+sql2000怎么连接啊?
- 如何制作一个背景透明的自定义控件啊?
- 怎样才能把MSChart显示时的网格线去掉?在线等待!!
- VB中,怎样实现有一个窗体无论上面打开多少窗体,总是激活的?
- 关于INI文件的问题(在线等待),请各位高手指导。
- 小笑话,大家开心一下
- 用vb如何调用delphi写的api dll?
- 关于代理服务器
- 如何得到sheet的名字?
Range("A" & begrow + 1 & ":" & "I" & begrow + R - 1).Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With Selection.Borders(xlEdgeLeft)
这些都要在前面加一个"点",一定要记着,因为如果不引用EXCEL的话就会出错.我碰到过.
.Range("A" & begrow + 1 & ":" & "I" & begrow + R).Select
.Range("A" & begrow + 1 & ":" & "I" & begrow + R - 1).Select