G
Glen Wood
I have tried several variations of the following function, but can't seem to
get excel to quit at the end if I have all the excel formating in place. It
seems to lose it's connection to excel. Someone got any ideas?
Function convert_files() As Boolean
On Error Resume Next
DoCmd.SetWarnings False
Dim xl As Object
Set xl = CreateObject("Excel.application")
xl.Application.Visible = False
xl.Application.UserControl = False
xl.DisplayAlerts = False
xl.Interactive = False
xl.ScreenUpdating = False
Dim xlbook As Object
Dim xlsheet As Object
Dim fs, f, f1, fc, s, wk, firsttime, tn, fn, qt, ch
Dim mindate, maxdata As Date
Let firsttime = True
Let qt = "Query_template"
Set fs = CreateObject("Scripting.FileSystemObject")
'
' clear archive directory
'
fs.deletefile "C:\KH\Archived\*.csv", True
'
' setup directory search for csv files
'
Set f = fs.GetFolder("C:\KH")
Set fc = f.Files
For Each f1 In fc
If Right(f1.Name, 3) = "csv" Then
'
' create xls file name from csv file namefrom template to create
xls file
'
Let tn = Mid(f1.Name, 4, 3) & Mid(f1.Name, 8, 4)
Let wk = "C:\KH\LD_" & tn & Format(f1.DateCreated, "yyyymmdd") &
".xls"
'
' create a work table from the table template 1 time
'
If firsttime = True Then
DoCmd.CopyObject , "work_table", acTable, "table_template"
Let firsttime = False
End If
'
' load the work table from the csv file
'
Let fn = "C:\KH\" & f1.Name
DoCmd.TransferText acImportDelim, "KH Import Specification",
"work_table", fn, True
'
' export the work table to the xls file using the copy of the
query template
'
DoCmd.CopyObject , tn, acQuery, qt
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, tn, wk,
True
DoCmd.DeleteObject acQuery, tn
'
' get the Min and Max check dates on the file
'
Let mindate = DMin("[check date]", "work_table")
Let maxdate = DMax("[check date]", "work_table")
' MsgBox maxdate
'
' open the xls file
'
Set xlbook = xl.Workbooks.Open(wk)
Set xlsheet = xl.ActiveSheet
'
' for debugging
'
' x = xlsheet.Name
' MsgBox x
' x = xlsheet.Name
' MsgBox x
'
' freeze row 1 for display
'
xlsheet.Activate
xlsheet.Range("A2").Select
ActiveWindow.FreezePanes = True
'
' bold row 1
'
With xlsheet
For Each cell In xlsheet.Range("A1", "O1")
cell.Font.Bold = True
Next
End With
'
' autosize row 1
'
With xlsheet
.Columns("A:N").Font.Name = "Arial"
.Columns("A:N").Font.Size = 9
.Columns("A:N").EntireColumn.AutoFit
End With
'
' set headers and footers
'
With xlsheet.PageSetup
.PrintTitleRows = xlsheet.Rows(1).Address
.PrintTitleColumns = xlsheet.Columns("A:O").Address
.Orientation = xlLandscape
.FitToPagesTall = False
.FitToPagesWide = 1
' .CenterHeader = "Labor Distribution " & Chr(10) & "for" &
Chr(10) & "&A"
.LeftHeader = "&B" & "Check Dates " & mindate & " to " & maxdate
& "&B"
.CenterHeader = "&B" & "Labor Distribution for " & "&A" & "&B"
.CenterFooter = "&F" & " " & "&D"
.RightFooter = "&R Page &P of &N"
.LeftMargin = xl.InchesToPoints(0)
.RightMargin = xl.InchesToPoints(0)
.TopMargin = xl.InchesToPoints(0.5)
.BottomMargin = xl.InchesToPoints(0.5)
.HeaderMargin = xl.InchesToPoints(0.25)
.FooterMargin = xl.InchesToPoints(0.25)
End With
'
' save and close workbook
'
xlbook.Save
xlbook.Close
Set xlsheet = Nothing
Set xlbook = Nothing
Let fm = "C:\KH\archived\" & f1.Name
' s = s & fn
' s = s & vbCrLf
fs.movefile fn, fm
DoCmd.OpenQuery "erase_work_table"
End If
Next
' MsgBox s
DoCmd.DeleteObject acTable, "work_table"
'
' cleanup
'
xl.Quit
Set xl = Nothing
Set xl = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
MsgBox "Excel not found " & Err.Number & " " & Err.Description
Else
MsgBox "Excel found " & Err.Number & " " & Err.Description
xl.Quit
Set xl = Nothing
End If
Set f = Nothing
Set fc = Nothing
Set fs = Nothing
End Function
get excel to quit at the end if I have all the excel formating in place. It
seems to lose it's connection to excel. Someone got any ideas?
Function convert_files() As Boolean
On Error Resume Next
DoCmd.SetWarnings False
Dim xl As Object
Set xl = CreateObject("Excel.application")
xl.Application.Visible = False
xl.Application.UserControl = False
xl.DisplayAlerts = False
xl.Interactive = False
xl.ScreenUpdating = False
Dim xlbook As Object
Dim xlsheet As Object
Dim fs, f, f1, fc, s, wk, firsttime, tn, fn, qt, ch
Dim mindate, maxdata As Date
Let firsttime = True
Let qt = "Query_template"
Set fs = CreateObject("Scripting.FileSystemObject")
'
' clear archive directory
'
fs.deletefile "C:\KH\Archived\*.csv", True
'
' setup directory search for csv files
'
Set f = fs.GetFolder("C:\KH")
Set fc = f.Files
For Each f1 In fc
If Right(f1.Name, 3) = "csv" Then
'
' create xls file name from csv file namefrom template to create
xls file
'
Let tn = Mid(f1.Name, 4, 3) & Mid(f1.Name, 8, 4)
Let wk = "C:\KH\LD_" & tn & Format(f1.DateCreated, "yyyymmdd") &
".xls"
'
' create a work table from the table template 1 time
'
If firsttime = True Then
DoCmd.CopyObject , "work_table", acTable, "table_template"
Let firsttime = False
End If
'
' load the work table from the csv file
'
Let fn = "C:\KH\" & f1.Name
DoCmd.TransferText acImportDelim, "KH Import Specification",
"work_table", fn, True
'
' export the work table to the xls file using the copy of the
query template
'
DoCmd.CopyObject , tn, acQuery, qt
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, tn, wk,
True
DoCmd.DeleteObject acQuery, tn
'
' get the Min and Max check dates on the file
'
Let mindate = DMin("[check date]", "work_table")
Let maxdate = DMax("[check date]", "work_table")
' MsgBox maxdate
'
' open the xls file
'
Set xlbook = xl.Workbooks.Open(wk)
Set xlsheet = xl.ActiveSheet
'
' for debugging
'
' x = xlsheet.Name
' MsgBox x
' x = xlsheet.Name
' MsgBox x
'
' freeze row 1 for display
'
xlsheet.Activate
xlsheet.Range("A2").Select
ActiveWindow.FreezePanes = True
'
' bold row 1
'
With xlsheet
For Each cell In xlsheet.Range("A1", "O1")
cell.Font.Bold = True
Next
End With
'
' autosize row 1
'
With xlsheet
.Columns("A:N").Font.Name = "Arial"
.Columns("A:N").Font.Size = 9
.Columns("A:N").EntireColumn.AutoFit
End With
'
' set headers and footers
'
With xlsheet.PageSetup
.PrintTitleRows = xlsheet.Rows(1).Address
.PrintTitleColumns = xlsheet.Columns("A:O").Address
.Orientation = xlLandscape
.FitToPagesTall = False
.FitToPagesWide = 1
' .CenterHeader = "Labor Distribution " & Chr(10) & "for" &
Chr(10) & "&A"
.LeftHeader = "&B" & "Check Dates " & mindate & " to " & maxdate
& "&B"
.CenterHeader = "&B" & "Labor Distribution for " & "&A" & "&B"
.CenterFooter = "&F" & " " & "&D"
.RightFooter = "&R Page &P of &N"
.LeftMargin = xl.InchesToPoints(0)
.RightMargin = xl.InchesToPoints(0)
.TopMargin = xl.InchesToPoints(0.5)
.BottomMargin = xl.InchesToPoints(0.5)
.HeaderMargin = xl.InchesToPoints(0.25)
.FooterMargin = xl.InchesToPoints(0.25)
End With
'
' save and close workbook
'
xlbook.Save
xlbook.Close
Set xlsheet = Nothing
Set xlbook = Nothing
Let fm = "C:\KH\archived\" & f1.Name
' s = s & fn
' s = s & vbCrLf
fs.movefile fn, fm
DoCmd.OpenQuery "erase_work_table"
End If
Next
' MsgBox s
DoCmd.DeleteObject acTable, "work_table"
'
' cleanup
'
xl.Quit
Set xl = Nothing
Set xl = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
MsgBox "Excel not found " & Err.Number & " " & Err.Description
Else
MsgBox "Excel found " & Err.Number & " " & Err.Description
xl.Quit
Set xl = Nothing
End If
Set f = Nothing
Set fc = Nothing
Set fs = Nothing
End Function