HeelNGville
Registered User.
- Local time
- Today, 10:46
- Joined
- Apr 13, 2004
- Messages
- 71
Hello, thanks in advance for any assistance. I have DB with a module that exports a table 'tbl_test' to a MS Excel File 'Load File,xlsx' and launches MS Excel, formats and saves the file based on a set of values. This works great (thanks to the unknown user that created) when the file is saved in the directory with the DB, however I need to write a second copy to a separate directory that the active DB is not stored. A copy of the destination file is located at T:\Planning\Load File.xlsx and this is where I need this code to write 'tbl_test' to. My limited coding skills has failed miserable, so any help in achieving above would be appreciated. Thank you!
Code below:
Public Function TferTbls()
' strTQName is the name of the table or query you want to send to Excel
' strSheetName is the name of the sheet you want to send it to
' strFilePath is the name and path of the file you want to send this data into.
Dim rst As DAO.Recordset
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim nfile As String
Dim strPath As String
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
'/commented for testing purposes
' On Error GoTo err_handler
'strPath = strFilePath
Set rst = CurrentDb.OpenRecordset("tbl_Test")
'/Is there anything to export?
If Not rst.EOF And Not rst.BOF Then
nfile = CurrentProject.Path & "\" & rst(0) & "-" & rst(1) & ".xlsx"
Else
MsgBox "No data in recordset", , "Export abandoned"
Exit Function
End If
'/ Does nfile already exist in the nominated location?
If Dir(nfile) <> "" Then
'delete the previous file
Kill nfile
End If
Set ApXL = CreateObject("Excel.Application")
Set xlWBk = ApXL.Workbooks.Open(CurrentProject.Path & "\Load File.xlsx")
'ApXL.Visible = True
Set xlWSh = xlWBk.Worksheets("Group")
xlWSh.Range("A1").Select
For Each fld In rst.Fields
ApXL.ActiveCell = fld.Name
ApXL.ActiveCell.Offset(0, 1).Select
Next
rst.MoveFirst
xlWSh.Range("A2").CopyFromRecordset rst
xlWSh.Range("1:1").Select
' This is included to show some of what you can do about formatting. You can comment out or delete
' any of this that you don't want to use in your own export.
With ApXL.Selection.Font
.Name = "Verdana"
.Size = 8
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
End With
ApXL.Selection.Font.Bold = True
With ApXL.Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.MergeCells = False
End With
' selects all of the cells
ApXL.ActiveSheet.Cells.Select
' does the "autofit" for all columns
ApXL.ActiveSheet.Cells.EntireColumn.AutoFit
' selects the first cell to unselect all cells
xlWBk.SaveAs nfile
xlWBk.Close
ApXL.Quit
Set ApXL = Nothing
rst.Close
Set rst = Nothing
Exit Function
err_handler:
DoCmd.SetWarnings True
MsgBox Err.Description, vbExclamation, Err.Number
Exit Function
End Function
Code below:
Public Function TferTbls()
' strTQName is the name of the table or query you want to send to Excel
' strSheetName is the name of the sheet you want to send it to
' strFilePath is the name and path of the file you want to send this data into.
Dim rst As DAO.Recordset
Dim ApXL As Object
Dim xlWBk As Object
Dim xlWSh As Object
Dim nfile As String
Dim strPath As String
Const xlCenter As Long = -4108
Const xlBottom As Long = -4107
'/commented for testing purposes
' On Error GoTo err_handler
'strPath = strFilePath
Set rst = CurrentDb.OpenRecordset("tbl_Test")
'/Is there anything to export?
If Not rst.EOF And Not rst.BOF Then
nfile = CurrentProject.Path & "\" & rst(0) & "-" & rst(1) & ".xlsx"
Else
MsgBox "No data in recordset", , "Export abandoned"
Exit Function
End If
'/ Does nfile already exist in the nominated location?
If Dir(nfile) <> "" Then
'delete the previous file
Kill nfile
End If
Set ApXL = CreateObject("Excel.Application")
Set xlWBk = ApXL.Workbooks.Open(CurrentProject.Path & "\Load File.xlsx")
'ApXL.Visible = True
Set xlWSh = xlWBk.Worksheets("Group")
xlWSh.Range("A1").Select
For Each fld In rst.Fields
ApXL.ActiveCell = fld.Name
ApXL.ActiveCell.Offset(0, 1).Select
Next
rst.MoveFirst
xlWSh.Range("A2").CopyFromRecordset rst
xlWSh.Range("1:1").Select
' This is included to show some of what you can do about formatting. You can comment out or delete
' any of this that you don't want to use in your own export.
With ApXL.Selection.Font
.Name = "Verdana"
.Size = 8
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
End With
ApXL.Selection.Font.Bold = True
With ApXL.Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.MergeCells = False
End With
' selects all of the cells
ApXL.ActiveSheet.Cells.Select
' does the "autofit" for all columns
ApXL.ActiveSheet.Cells.EntireColumn.AutoFit
' selects the first cell to unselect all cells
xlWBk.SaveAs nfile
xlWBk.Close
ApXL.Quit
Set ApXL = Nothing
rst.Close
Set rst = Nothing
Exit Function
err_handler:
DoCmd.SetWarnings True
MsgBox Err.Description, vbExclamation, Err.Number
Exit Function
End Function