Solved

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

Posted on 2016-11-14
7
68 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 47

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 47

Expert Comment

by:Martin Liss
ID: 41886665
Try
XLSBK.Workbooks(1).Close
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 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