I'm trying to export an MS Form, and then edit it.
My code works fine the first time, but when I try it again I get errors.
It think its because the Excel application is not closing properly. I've searched all over to try get it to close, but its still always there in my task manager.
Everytime I run the code, there is another instance of Excel in the task manager, which tells me I'm not closing it properly, but I'm at a loss.
What am I missing?
After the first time running I get errors on the rows starting ".range" or ".selection"
Here is the code:
Private Sub BtnExport2_Click()
Dim xl As Object
Dim WB As Excel.Workbook
Dim WS As Excel.Worksheet
FullPath = DLookup("hyperlinkbase", "tblusers", "id = " & Forms!frmmain!UserName) & "\TodaysPayments.xls"
DoCmd.OutputTo acOutputForm, "frmexpenses", acFormatXLS, FullPath
Set xl = CreateObject("excel.application")
Set WB = xl.workbooks.Open(FullPath)
Set WS = WB.sheets("frmexpenses")
With xl
.columns("A:B").Delete
.columns("D:E").Delete
.columns("E:E").Delete
.columns("F:I").Delete
.Range(Range("E1"), Range("E1").End(xlDown)).Select
.Selection.Offset(0, 1).Select
.Selection.Value = "=C1 & ""-"" & A1"
.Selection.Copy
.Selection.Offset(0, 1).PasteSpecial xlPasteValues
.columns("A:A").Delete
.columns("B:B").Delete
.columns("D").Delete
.columns("d").Select
.Selection.Cut
.columns("B:B").Select
.Selection.Insert Shift:=xlToRight
.columns("A:B").ColumnWidth = 30
.Range("A11").Interior.Color = RGB(221, 221, 221)
.Range("A11").Borders.Color = RGB(0, 0, 0)
.Range("A1").Select
End With
xl.Visible = True
WB.Close
xl.Quit
Set WS = Nothing
Set WB = Nothing
Set xl = Nothing
End Sub
My code works fine the first time, but when I try it again I get errors.
It think its because the Excel application is not closing properly. I've searched all over to try get it to close, but its still always there in my task manager.
Everytime I run the code, there is another instance of Excel in the task manager, which tells me I'm not closing it properly, but I'm at a loss.
What am I missing?
After the first time running I get errors on the rows starting ".range" or ".selection"
Here is the code:
Private Sub BtnExport2_Click()
Dim xl As Object
Dim WB As Excel.Workbook
Dim WS As Excel.Worksheet
FullPath = DLookup("hyperlinkbase", "tblusers", "id = " & Forms!frmmain!UserName) & "\TodaysPayments.xls"
DoCmd.OutputTo acOutputForm, "frmexpenses", acFormatXLS, FullPath
Set xl = CreateObject("excel.application")
Set WB = xl.workbooks.Open(FullPath)
Set WS = WB.sheets("frmexpenses")
With xl
.columns("A:B").Delete
.columns("D:E").Delete
.columns("E:E").Delete
.columns("F:I").Delete
.Range(Range("E1"), Range("E1").End(xlDown)).Select
.Selection.Offset(0, 1).Select
.Selection.Value = "=C1 & ""-"" & A1"
.Selection.Copy
.Selection.Offset(0, 1).PasteSpecial xlPasteValues
.columns("A:A").Delete
.columns("B:B").Delete
.columns("D").Delete
.columns("d").Select
.Selection.Cut
.columns("B:B").Select
.Selection.Insert Shift:=xlToRight
.columns("A:B").ColumnWidth = 30
.Range("A11").Interior.Color = RGB(221, 221, 221)
.Range("A11").Borders.Color = RGB(0, 0, 0)
.Range("A1").Select
End With
xl.Visible = True
WB.Close
xl.Quit
Set WS = Nothing
Set WB = Nothing
Set xl = Nothing
End Sub