I use the following code to create an Excel file. This works great the first time, but if I reopen the form and click on Command 125 I receive an error "Object variable or With block variable not set"
When I close Access and reopen the form, it functions fine.
What am I missing?
Thanks, Bill
Private Sub Command125_Click()
Dim objExcel As Excel.Application, xlWS As Object
Dim FS As Object
Dim sFileName As String
On Error GoTo Err_Command125_Click
sFileName = "V:\Excel\INVOICE.xls"
Set FS = CreateObject("Scripting.FileSystemObject")
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open sFileName
Set xlWS = objExcel.Worksheets("Simple Invoice")
xlWS.[C1].Cells(5) = Me.Text93
xlWS.[B1].Cells(39) = Me.Agent & ", Title Agent."
xlWS.[A1].Cells(10) = Me.Text95
xlWS.[A1].Cells(11) = Me.Text97
xlWS.[A1].Cells(12) = Me.Text98
ActiveWorkbook.SaveAs FileName:="V:\CALLIE'S OPEN\" & Me.Text93 & "\" & Me.Text93 & "_inv.xls", FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False
objExcel.Visible = True
Screen.MousePointer = 0
Set xlWS = Nothing
objExcel.Quit
Set objExcel = Nothing
Set FS = Nothing
DoCmd.Close acForm, "INV_E"
DoCmd.Close acForm, "Output"
DoCmd.OpenForm "frmStart_Page"
Exit_Command125_Click:
Exit Sub
Err_Command125_Click:
Set xlWS = Nothing
objExcel.Quit
Set objExcel = Nothing
Set FS = Nothing
Screen.MousePointer = 0 'normal
MsgBox Err.Description
Resume Exit_Command125_Click
End Sub
When I close Access and reopen the form, it functions fine.
What am I missing?
Thanks, Bill
Private Sub Command125_Click()
Dim objExcel As Excel.Application, xlWS As Object
Dim FS As Object
Dim sFileName As String
On Error GoTo Err_Command125_Click
sFileName = "V:\Excel\INVOICE.xls"
Set FS = CreateObject("Scripting.FileSystemObject")
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open sFileName
Set xlWS = objExcel.Worksheets("Simple Invoice")
xlWS.[C1].Cells(5) = Me.Text93
xlWS.[B1].Cells(39) = Me.Agent & ", Title Agent."
xlWS.[A1].Cells(10) = Me.Text95
xlWS.[A1].Cells(11) = Me.Text97
xlWS.[A1].Cells(12) = Me.Text98
ActiveWorkbook.SaveAs FileName:="V:\CALLIE'S OPEN\" & Me.Text93 & "\" & Me.Text93 & "_inv.xls", FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False
objExcel.Visible = True
Screen.MousePointer = 0
Set xlWS = Nothing
objExcel.Quit
Set objExcel = Nothing
Set FS = Nothing
DoCmd.Close acForm, "INV_E"
DoCmd.Close acForm, "Output"
DoCmd.OpenForm "frmStart_Page"
Exit_Command125_Click:
Exit Sub
Err_Command125_Click:
Set xlWS = Nothing
objExcel.Quit
Set objExcel = Nothing
Set FS = Nothing
Screen.MousePointer = 0 'normal
MsgBox Err.Description
Resume Exit_Command125_Click
End Sub