dinh_trong_hoa > 24-04-14, 09:26 PM
Public Sub export()
Dim rs As Recordset
Dim ex As Excel.Application
Dim wb As Workbook
Dim ws As Worksheet
On Error GoTo Err_ExcelOpen_Click
Set rs1 = CurrentDb.OpenRecordset("ten_query", dbOpenDynaset)
Set ex = CreateObject("Excel.Application")
ex.Visible = True
Set wb = ex.Workbooks.Open(CurrentProject.Path & "\ten_file.xls")
Set ws = wb.Worksheets("ten_sheet")
Set Rng = ws.Cells(6, 1)
Rng.CopyFromRecordset rs
ws.Columns.EntireColumn.AutoFit
ws.Rows.EntireRow.AutoFit
ws.Range(ws.Cells(6, 1), ws.Cells(rs.RecordCount + 6, 8)).Select
ex.Selection.Borders(xlDiagonalDown).LineStyle = xlNone
ex.Selection.Borders(xlDiagonalUp).LineStyle = xlNone
With ex.Selection.Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With ex.Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With ex.Selection.Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With ex.Selection.Borders(xlEdgeRight)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With ex.Selection.Borders(xlInsideVertical)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With ex.Selection.Borders(xlInsideHorizontal)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With ex.ActiveSheet.PageSetup
.LeftMargin = ex.Application.InchesToPoints(0.4)
.RightMargin = ex.Application.InchesToPoints(0.4)
.TopMargin = ex.Application.InchesToPoints(0.4)
.BottomMargin = ex.Application.InchesToPoints(0.4)
.HeaderMargin = ex.Application.InchesToPoints(0.4)
.FooterMargin = ex.Application.InchesToPoints(0.4)
.Orientation = xlLandscape
.PrintQuality = 300
.CenterHorizontally = True
.PaperSize = xlPaperA4
End With
Err_ExcelOpen_Exit:
Set wb = Nothing
Set ws = Nothing
Exit Sub
Err_ExcelOpen_Click:
If Err.Number = 13 Then
Resume Next
Else
MsgBox Err.Description
Resume Err_ExcelOpen_Exit
End If
End Sub