Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Access to Excel problems 2

Status
Not open for further replies.

bill420

IS-IT--Management
Oct 12, 2005
23
US
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
 
Replace this:
ActiveWorkbook.SaveAs
with this:
objExcel.ActiveWorkbook.SaveAs

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you once again for your insight.
Bill
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top