Link to home
Start Free TrialLog in
Avatar of Nitin Shah
Nitin ShahFlag for India

asked on

vb6, excel Run-time error 1004 method 'close' of object 'workbooks' failed

I have following code in VB6 application where i open file for reading and storing values in dimension. i close the file and open file again for updating values. but while closing it give error  Run-time error 1004  Method 'Close' of object 'Workbooks' failed'

following is my code

    Dim XLSBK              As New Excel.Application
    Dim XLwsh              As New Excel.Worksheet

    Dim fs
    Set fs = CreateObject("scripting.filesystemobject")
    SelParamFileName = Trim(ctlSELPARAMFILENAME.Text)
    If Trim(Len(SelParamFileName)) = 0 Then
       MsgBox "Select Excel Parameter file", vbCritical, Me.Caption
'       cmdPARAMSEARCH.SetFocus
       Exit Sub
    End If
'**********************************************************************
    If Not fs.FileExists(SelParamFileName) Then
        MsgBox "Warning - Not a valid Excel Parameter file name ", vbCritical
        Exit Sub
    End If
'**********************************************************************
    XLSBK.DefaultFilePath = SelParamFileName
'**********************************************************************
    XLSBK.Workbooks.Open SelParamFileName, , , , , , , , , True
    XLSBK.Visible = False
     For Ctr = 1 to 20
                XLSBK.Cells(Ctr, 11) = "updateremark"
      next        
 
     XLSBK.Workbooks.Close ( ON THIS LINE SYSTEM GOT HANG AND GIVE MESSAGE RETRY OR SWITCH TO - WHEN PRESS CLT+ALT+DEL
       TO TERMINATE PROCESS - ERROR 1004 )



Thanks in advance .
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

XLSBK.Workbooks(SelParamFileName).Close
Avatar of Nitin Shah

ASKER

Dear Martin Liss

XLSBK.Workbooks(SelParamFileName).Close  

is giving error - Subscript out range
Try
XLSBK.Workbooks(1).Close
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dear Ryan Chong

It is working file but at instruction

XLSworkbook.Save - It give message ' A file namned 'RESUME.XLW' already exists in this location. do want to replace it?
yes - no - cancel


when i say YES it is saving in same file. but i could not find file name in my computer -  'RESUME.XLW'

thanks sir
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Ryan Chong,


It is working fine.

Thanks for kind prompt reply.

Thanks