Solved

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

Posted on 2016-11-14
7
50 Views
Last Modified: 2016-11-15
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 .
0
Comment
Question by:nitin_s_shah
  • 3
  • 2
  • 2
7 Comments
 
LVL 46

Expert Comment

by:Martin Liss
ID: 41886637
XLSBK.Workbooks(SelParamFileName).Close
0
 

Author Comment

by:nitin_s_shah
ID: 41886657
Dear Martin Liss

XLSBK.Workbooks(SelParamFileName).Close  

is giving error - Subscript out range
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 41886665
Try
XLSBK.Workbooks(1).Close
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 50

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41887250
or you can define a Excel.Workbook object , like:

Dim XLSworkbook              As New Excel.Workbook
...

set XLSworkbook = XLSBK.Workbooks.Open(SelParamFileName, , , , , , , , , True)
    XLSworkbook.Visible = False
Set XLwsh = XLSworkbook.WorkSheets(1)
     For Ctr = 1 to 20
                XLwsh.Cells(Ctr, 11) = "updateremark"
      next        
     XLSworkbook.Save
     XLSworkbook.Close
Set XLwsh = Nothing
Set XLSworkbook = Nothing
....

Open in new window

0
 

Author Comment

by:nitin_s_shah
ID: 41887798
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
0
 
LVL 50

Assisted Solution

by:Ryan Chong
Ryan Chong earned 500 total points
ID: 41889060
if problem persists... you may try use SaveAs instead:
XLSworkbook.SaveAs SelParamFileName

Open in new window

I'm not too sure about the .xlw issue, but it seems linked to excel workspace?
0
 

Author Closing Comment

by:nitin_s_shah
ID: 41889247
Thanks Ryan Chong,


It is working fine.

Thanks for kind prompt reply.

Thanks
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question