Solved

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

Posted on 2016-11-14
7
33 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 45

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 45

Expert Comment

by:Martin Liss
ID: 41886665
Try
XLSBK.Workbooks(1).Close
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 49

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 49

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now