Solved

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

Posted on 2016-11-14
7
56 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 51

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 51

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

856 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