J
Joker via AccessMonster.com
Hello,
I have a database that is opening excel and doing a few things with it. My
problem comes at the end it doesn't want to close the Excel process so if the
dB is run again it returns an error. My code is below. Thanks.
Option Compare Database
Option Explicit
Function ExcelFormat()
On Error GoTo ErrHnd_Err
'***************************************************************************
' Variables
'***************************************************************************
Dim intResponse As Integer
Dim strFolder As String
Dim strSQL As String
Dim strTblName As String
Dim dbs As Database
Dim rst As Recordset
Dim rst1 As Recordset
Dim appXL As Excel.Application
Dim wbkXL As Excel.Workbook
Dim shtXL As Excel.Worksheet
Dim i As Integer
Dim Z As Integer
Dim strFilter As String
Dim strInputFileName As String
DoCmd.SetWarnings False
DoCmd.SetWarnings False
Set dbs = CurrentDb
'***************************************************************************
' Select Cutoff File
'***************************************************************************
'This is where the dialog box opens to select the cutoff file.
strFilter = ahtAddFilterItem(strFilter, "Spreadsheets (.xls)", "*.xls")
strInputFileName = ahtCommonFileOpenSave(Filter:=strFilter, OpenFile:
=True, DialogTitle:="Select a Cutoff file for the invesor you are running",
Flags:=ahtOFN_HIDEREADONLY)
'***************************************************************************
' Excel Functionality for Import
'***************************************************************************
Set appXL = CreateObject("Excel.Application")
appXL.DisplayAlerts = False
'USE FOR DEBUGGING ONLY
'appXL.Visible = True
'Open File in Excel and delete uneeded stuff as well as replaces functions
with actual values
appXL.Workbooks.Open strInputFileName
Set wbkXL = appXL.ActiveWorkbook
Set shtXL = wbkXL.ActiveSheet
With shtXL
.Rows("1:5").Select
.Range("A5").Activate
.Rows("1").Delete
.Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
.Rows("6:6").Select
.Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
.Rows("1:4").Select
.Range("A4").Activate
Selection.Delete Shift:=xlUp
'***************************************************************************
' Saves Cutoff file to temp
'***************************************************************************
'Saves the new file to the temp folder to be imported the db then deleted
ChDir "C:\Temp"
ActiveWorkbook.SaveAs FileName:="C:\Temp\PTSExcelFile.xls", FileFormat:=
_
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:
=False _
, CreateBackup:=False
End With
DoCmd.SetWarnings False
'***************************************************************************
' Check to see if the table exists, if so delete it
'***************************************************************************
If IsTableQuery("", "tbl_CutoffImport") Then
DoCmd.DeleteObject acTable, "tbl_CutoffImport"
Else
GoTo SKIPALONG
End If
SKIPALONG:
DoCmd.SetWarnings True
'***************************************************************************
' Transfer Spreadsheet to dB
'***************************************************************************
'DoCmd.TransferSpreadsheet acImport, 8, "tbl_CutoffImport", strInputFileName,
True, ""
DoCmd.TransferSpreadsheet acImport, 8, "tbl_CutoffImport", "C:\Temp\
PTSExcelFile.xls", True, ""
GoTo Closing
ErrHnd_Err:
MsgBox ("Error Handler")
Resume Closing
Closing:
'***************************************************************************
' Close and exit Excel
'***************************************************************************
ActiveWorkbook.Close
Set dbs = Nothing
'wbkXL.Close
Set wbkXL = Nothing
Set shtXL = Nothing
appXL.Quit
Set appXL = Nothing
DoCmd.SetWarnings True
End Function
I have a database that is opening excel and doing a few things with it. My
problem comes at the end it doesn't want to close the Excel process so if the
dB is run again it returns an error. My code is below. Thanks.
Option Compare Database
Option Explicit
Function ExcelFormat()
On Error GoTo ErrHnd_Err
'***************************************************************************
' Variables
'***************************************************************************
Dim intResponse As Integer
Dim strFolder As String
Dim strSQL As String
Dim strTblName As String
Dim dbs As Database
Dim rst As Recordset
Dim rst1 As Recordset
Dim appXL As Excel.Application
Dim wbkXL As Excel.Workbook
Dim shtXL As Excel.Worksheet
Dim i As Integer
Dim Z As Integer
Dim strFilter As String
Dim strInputFileName As String
DoCmd.SetWarnings False
DoCmd.SetWarnings False
Set dbs = CurrentDb
'***************************************************************************
' Select Cutoff File
'***************************************************************************
'This is where the dialog box opens to select the cutoff file.
strFilter = ahtAddFilterItem(strFilter, "Spreadsheets (.xls)", "*.xls")
strInputFileName = ahtCommonFileOpenSave(Filter:=strFilter, OpenFile:
=True, DialogTitle:="Select a Cutoff file for the invesor you are running",
Flags:=ahtOFN_HIDEREADONLY)
'***************************************************************************
' Excel Functionality for Import
'***************************************************************************
Set appXL = CreateObject("Excel.Application")
appXL.DisplayAlerts = False
'USE FOR DEBUGGING ONLY
'appXL.Visible = True
'Open File in Excel and delete uneeded stuff as well as replaces functions
with actual values
appXL.Workbooks.Open strInputFileName
Set wbkXL = appXL.ActiveWorkbook
Set shtXL = wbkXL.ActiveSheet
With shtXL
.Rows("1:5").Select
.Range("A5").Activate
.Rows("1").Delete
.Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
.Rows("6:6").Select
.Application.CutCopyMode = False
Selection.Delete Shift:=xlUp
.Rows("1:4").Select
.Range("A4").Activate
Selection.Delete Shift:=xlUp
'***************************************************************************
' Saves Cutoff file to temp
'***************************************************************************
'Saves the new file to the temp folder to be imported the db then deleted
ChDir "C:\Temp"
ActiveWorkbook.SaveAs FileName:="C:\Temp\PTSExcelFile.xls", FileFormat:=
_
xlNormal, Password:="", WriteResPassword:="", ReadOnlyRecommended:
=False _
, CreateBackup:=False
End With
DoCmd.SetWarnings False
'***************************************************************************
' Check to see if the table exists, if so delete it
'***************************************************************************
If IsTableQuery("", "tbl_CutoffImport") Then
DoCmd.DeleteObject acTable, "tbl_CutoffImport"
Else
GoTo SKIPALONG
End If
SKIPALONG:
DoCmd.SetWarnings True
'***************************************************************************
' Transfer Spreadsheet to dB
'***************************************************************************
'DoCmd.TransferSpreadsheet acImport, 8, "tbl_CutoffImport", strInputFileName,
True, ""
DoCmd.TransferSpreadsheet acImport, 8, "tbl_CutoffImport", "C:\Temp\
PTSExcelFile.xls", True, ""
GoTo Closing
ErrHnd_Err:
MsgBox ("Error Handler")
Resume Closing
Closing:
'***************************************************************************
' Close and exit Excel
'***************************************************************************
ActiveWorkbook.Close
Set dbs = Nothing
'wbkXL.Close
Set wbkXL = Nothing
Set shtXL = Nothing
appXL.Quit
Set appXL = Nothing
DoCmd.SetWarnings True
End Function