Hi
The following function takes table and sends to excel.
What I need to do is carry put border outside and inside for all output result.
Can you please help me append code to do that.
Thank you for your help
Function SendToExcelDataChecks(strTableUsing As String, strTitle As String)
On Error GoTo SendToExcel_Fail
Dim objWS As Excel.Worksheet
Dim rstData As ADODB.Recordset
Dim rstCount As ADODB.Recordset
Dim fld As ADODB.Field
Dim intColCount As Integer
Dim intRowCount As Integer
Set rstData = New ADODB.Recordset
rstData.ActiveConnection = CurrentProject.Connection
Set rstCount = New ADODB.Recordset
rstCount.ActiveConnection = CurrentProject.Connection
'Invoke HourGlass
DoCmd.Hourglass True
'Try to create recordset and create Excel Object
If CreateRecordSet(rstData, rstCount, strTableUsing) Then
If CreateExcelObj() Then
'add a workbooks
With gobjExcel
'With gobjExcel
'Create a reference to the active sheet
With gobjExcel.Sheets(1)
gobjExcel.Sheets(1).Cells.ClearContents
intRowCount = 1
intColCount = 1
'Loop through the fields collection
'make each field name a collumn heading in excel
For Each fld In rstData.Fields
If fld.Type <> adLongVarBinary Then
.Cells(1, intColCount).Value = fld.Name
intColCount = intColCount + 1
End If
Next fld
.Range("A2").CopyFromRecordset rstData, 16383
.Range("A1").CurrentRegion.Copy
End With
'hide all three sheets
'gobjExcel.Sheets(1).Visible = True
'gobjExcel.Sheets(2).Visible = False
'gobjExcel.Sheets(3).Visible = False
'add a sheet in that LAST tab position
With .Worksheets.Add(After:=.Sheets(.Worksheets.Count))
.Range("B5").PasteSpecial Paste:=xlPasteAll, Transpose:=False
'.Name = strSheetName
.[c2].Value = strTitle
.[c2].EntireRow.Font.Bold = True
.[c2].EntireRow.Font.Size = 10
.[c2].EntireRow.Font.Name = "Arial"
.[c2].HorizontalAlignment = xlCenter
.Cells.EntireColumn.ColumnWidth = 30
.[B5].EntireRow.Font.Bold = True
.[B5].EntireRow.HorizontalAlignment = xlCenter
.Cells.EntireRow.Font.Size = 10
.Cells.EntireRow.Font.Name = "Arial"
.Cells.EntireRow.HorizontalAlignment = xlCenter
'autofit the columns
.Cells.EntireColumn.AutoFit
'gobjExcel.Sheets(1).Visible = False
End With
End With
Else
MsgBox "Excel not Successfully Launched", vbInformation
End If
End If
Exit_SendToExcel:
DoCmd.Hourglass False
Set objWS = Nothing
Set rstCount = Nothing
Set rstData = Nothing
Set fld = Nothing
Set gobjExcel = Nothing
Exit Function
SendToExcel_Fail:
MsgBox "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & Err.Description
Resume Exit_SendToExcel
End Function
The following function takes table and sends to excel.
What I need to do is carry put border outside and inside for all output result.
Can you please help me append code to do that.
Thank you for your help
Function SendToExcelDataChecks(strTableUsing As String, strTitle As String)
On Error GoTo SendToExcel_Fail
Dim objWS As Excel.Worksheet
Dim rstData As ADODB.Recordset
Dim rstCount As ADODB.Recordset
Dim fld As ADODB.Field
Dim intColCount As Integer
Dim intRowCount As Integer
Set rstData = New ADODB.Recordset
rstData.ActiveConnection = CurrentProject.Connection
Set rstCount = New ADODB.Recordset
rstCount.ActiveConnection = CurrentProject.Connection
'Invoke HourGlass
DoCmd.Hourglass True
'Try to create recordset and create Excel Object
If CreateRecordSet(rstData, rstCount, strTableUsing) Then
If CreateExcelObj() Then
'add a workbooks
With gobjExcel
'With gobjExcel
'Create a reference to the active sheet
With gobjExcel.Sheets(1)
gobjExcel.Sheets(1).Cells.ClearContents
intRowCount = 1
intColCount = 1
'Loop through the fields collection
'make each field name a collumn heading in excel
For Each fld In rstData.Fields
If fld.Type <> adLongVarBinary Then
.Cells(1, intColCount).Value = fld.Name
intColCount = intColCount + 1
End If
Next fld
.Range("A2").CopyFromRecordset rstData, 16383
.Range("A1").CurrentRegion.Copy
End With
'hide all three sheets
'gobjExcel.Sheets(1).Visible = True
'gobjExcel.Sheets(2).Visible = False
'gobjExcel.Sheets(3).Visible = False
'add a sheet in that LAST tab position
With .Worksheets.Add(After:=.Sheets(.Worksheets.Count))
.Range("B5").PasteSpecial Paste:=xlPasteAll, Transpose:=False
'.Name = strSheetName
.[c2].Value = strTitle
.[c2].EntireRow.Font.Bold = True
.[c2].EntireRow.Font.Size = 10
.[c2].EntireRow.Font.Name = "Arial"
.[c2].HorizontalAlignment = xlCenter
.Cells.EntireColumn.ColumnWidth = 30
.[B5].EntireRow.Font.Bold = True
.[B5].EntireRow.HorizontalAlignment = xlCenter
.Cells.EntireRow.Font.Size = 10
.Cells.EntireRow.Font.Name = "Arial"
.Cells.EntireRow.HorizontalAlignment = xlCenter
'autofit the columns
.Cells.EntireColumn.AutoFit
'gobjExcel.Sheets(1).Visible = False
End With
End With
Else
MsgBox "Excel not Successfully Launched", vbInformation
End If
End If
Exit_SendToExcel:
DoCmd.Hourglass False
Set objWS = Nothing
Set rstCount = Nothing
Set rstData = Nothing
Set fld = Nothing
Set gobjExcel = Nothing
Exit Function
SendToExcel_Fail:
MsgBox "Error # " & Str(Err.Number) & " was generated by " _
& Err.Source & Chr(13) & Err.Description
Resume Exit_SendToExcel
End Function