You are on page 1of 3

UNKNOWN

'**************************************
'Windows API/Global Declarations for :Ex
' port Database to Excel Report
'**************************************
Please add a reference to
Microsoft Excel 9.0(or 8.0) object library.
'**************************************
' Name: Export Database to Excel Report
' Description:This is the code that expo
' rt a database on DAO (whatever what file

' type it is), to a excel report. In fact,


' this code is copied from a taiwanese boo
' k. Very Cool, please give comment on it,
' thank you.
' By: Kenny Lai, Lai Ho Wa
'
'
' Inputs:None
'
' Returns:None
'
'Assumes:None
'
'Side Effects:None
'This code is copyrighted and has limite
' d warranties.
'Please see http://www.Planet-Source-Cod
' e.com/xq/ASP/txtCodeId.24866/lngWId.1/qx
' /vb/scripts/ShowCode.htm
'for details.
'**************************************

Option Explicit

Public Sub ExportExcelReport(datTarget As Data, Optional strTitle As


String = "", Optional intInitialRow As Integer = 0)
On Error Resume Next
Dim objExcelApp As Excel.Application
Dim objSheet As Excel.Worksheet
Dim rstTarget As Recordset
Dim intTable As Integer, intStartField As Integer
Dim I As Integer, J As Integer, intNum As Integer
Dim intCurrentRow As Integer
Dim QuanIdx As Integer
Dim strEQ_Desc As String, strModel As String
Set objExcelApp = CreateObject("Excel.Application")
objExcelApp.Workbooks.Add

objExcelApp.Visible = True
objExcelApp.ActiveWorkbook.Worksheets.Add
Set objSheet = objExcelApp.ActiveWorkbook.ActiveSheet
intCurrentRow = intInitialRow
Set rstTarget = datTarget.Recordset.Clone

If rstTarget.RecordCount > 0 Then


objSheet.Cells(intCurrentRow, 1).Value = strTitle

For J = 0 To rstTarget.Fields.Count - 1
objSheet.Cells(intCurrentRow + 1, J + 1).Value =
rstTarget.Fields(J).Name
Next J
rstTarget.MoveLast
intNum = rstTarget.RecordCount
rstTarget.MoveFirst
I = intCurrentRow + 2

Do While Not rstTarget.EOF


objExcelApp.StatusBar = "Writing Record: " & I -
(intCurrentRow + 1) & " of " & intNum

For J = 0 To rstTarget.Fields.Count - 1
If Not IsNull(rstTarget.Fields(J)) Then
objSheet.Cells(I, J + 1).Value = rstTarget.Fields(J).Value
Next J
I = I + 1
rstTarget.MoveNext
Loop
objSheet.Range(objSheet.Cells(intCurrentRow + 1, 1),
objSheet.Cells(intCurrentRow + 1, rstTarget.Fields.Count)).Select
objExcelApp.Selection.EntireColumn.AutoFit
objExcelApp.Selection.Interior.ColorIndex = 24
objSheet.Range(objSheet.Cells(intCurrentRow + 1, 1),
objSheet.Cells(I - 1, rstTarget.Fields.Count)).Select

With objExcelApp.Selection
.Borders(7).Weight = 4
.Borders(7).ColorIndex = -4105
.Borders(8).Weight = 4
.Borders(8).ColorIndex = -4105
.Borders(9).Weight = 4
.Borders(9).ColorIndex = -4105
.Borders(10).Weight = 4
.Borders(10).ColorIndex = -4105
.Borders(11).Weight = 2
.Borders(11).ColorIndex = -4105

If (I - 1) - (intCurrentRow + 1) > 1 Then


.Borders(12).Weight = 2
.Borders(12).ColorIndex = -4105
End If
End With
With objSheet.PageSetup
.PrintTitleRows = "$" & intCurrentRow & ":$" & intCurrentRow
+ 1
.Orientation = 2
.Zoom = False
.FitToPagesWide = 1
.FitToPagesTall = 10
End With
rstTarget.Close
Set rstTarget = Nothing
End If
Screen.MousePointer = vbDefault
End Sub

You might also like