第一次导出可,第二次导出就出错。为什么?
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

解决方案 »

  1.   

    Range("A" & begrow + 1 & ":" & "I" & begrow + R).Select
    Range("A" & begrow + 1 & ":" & "I" & begrow + R - 1).Select
              Selection.Borders(xlDiagonalDown).LineStyle = xlNone
                Selection.Borders(xlDiagonalUp).LineStyle = xlNone
                With Selection.Borders(xlEdgeLeft)
    这些都要在前面加一个"点",一定要记着,因为如果不引用EXCEL的话就会出错.我碰到过.
      

  2.   

    要这样
    .Range("A" & begrow + 1 & ":" & "I" & begrow + R).Select
    .Range("A" & begrow + 1 & ":" & "I" & begrow + R - 1).Select