1

I use dos .bat file to run a .vbs file continuously ... If any error occurs the batch file runs the vbscript again and it goes on. The script connects via internet to a site to execute some api calls. Now when there is connection problem or any other error then the control comes out of the script keeping the excel file open. This way many excel files gets open on each error ... The code is as follows ... please advice me how to close the excel file on error and then come out of the script gracefully.

       '{{{{Some coding}}}}

dim objExcel, objWorkbook, objRange
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\temp.xlsx")
objExcel.Visible = True

       '{{{{Some coding}}}}

objExcel.Cells(xlrow, 3).Value="Test"
objExcel.Cells(xlrow, 3).Select
objExcel.Activecell.Show

       '{{{{Some coding}}}}

objExcel.Workbooks(1).save
objExcel.Workbooks(1).close
objExcel.Quit
Set objExcel = Nothing
Set objWorkbook = Nothing
WScript.Quit

Thanks in advance

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Parag
  • 13
  • 1
  • 4

1 Answers1

2

One possible approach is to wrap the Excel handling in a custom class:

Class Excel
  Private xl

  Private Sub Class_Initialize
    Set xl = CreateObject("Excel.Application")
  End Sub

  Private Sub Class_Terminate
    For Each wb In xl.Workbooks
      wb.Saved = True  'discard unsaved changes
      wb.Close         'close workbook
    Next
    xl.Quit            'quit Excel
  End Sub

  Public Function OpenWorkbook(filename)
    Set OpenWorkbook = xl.Workbooks.Open(filename)
  End Function

  Public Function NewWorkbook
    Set NewWorkbook = xl.Workbooks.Add
  End Function

  Public Property Get Workbooks
    Set Workbooks = xl.Workbooks
  End Property
End Class

The procedure Class_Terminate is automatically called whenever a class instance is destroyed. That way you can automatically close the open workbooks and quit Excel.

The class can be used like this:

Set xl = New Excel
Set wb = xl.OpenWorkbook("C:\path\to\your.xlsx")
...
wb.Close
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thanks for rewording the question. I misunderstood the OP. :) – Damien Jan 18 '14 at 15:41
  • Wow looks great ... sorry am a very newbie for vbscript ... but before I try should it be "end function" instead of "end sub" twice? Thanks! – Parag Jan 18 '14 at 17:56
  • Have a look at http://stackoverflow.com/a/20382898/603855 for an approach/workaround that avoids having to write (lots of) wrapper functions. P.S. I'm sure Ansgar will edit the glitches (End Function, return by assigning to function names). – Ekkehard.Horner Jan 18 '14 at 18:49
  • 1
    @user3209506 Yes, it should. Fixed. – Ansgar Wiechers Jan 19 '14 at 00:13
  • 1
    @AnsgarWiechers - what about the function returns? – Ekkehard.Horner Jan 19 '14 at 09:20
  • @Ekkehard.Horner ... I tested your example at http://stackoverflow.com/a/20382898/603855 ... and it worked exactly as i want it ... but i tried a lot to add codes to open an existing excel file and then save it normally if error doesnt occur ... but failed ...I know this will be a lot like spoon feeding ... but please can you give a updated version which will open an existing excel file and close it normally too? Thanks again! – Parag Jan 19 '14 at 09:40
  • @Ekkehard.Horner *\*slaps self\** Sorry, I obviously wasn't awake yesterday. Thanks for pointing that out. – Ansgar Wiechers Jan 19 '14 at 11:35
  • @Parag Using the code from [Ekkehard.Horner's answer](http://stackoverflow.com/a/20382898/1630171) you could open an existing workbook like this: `Set oWBook = oExcel.Obj.Workbooks.Open("C:\path\to\some.xlsx")`. The workbook can be closed as usual by calling its `Close` method: `oWBook.Close`. – Ansgar Wiechers Jan 19 '14 at 11:42
  • You both are the best ... one last thing can I uses this so that the file gets saved before quiting? ... Public Sub Class_Terminate() m_oExcel.Save m_oExcel.Quit End Sub ..... Thank you so much! – Parag Jan 19 '14 at 12:30
  • @Parag Yes, that should work. Note that newly created workbooks will be saved in the default file path (usually your `Documents` folder). – Ansgar Wiechers Jan 19 '14 at 12:36
  • So if I already have a previously created excel file ... should I omit this line? ... Dim oWBook : Set oWBook = oExcel.Obj.WorkBooks.Add() – Parag Jan 19 '14 at 12:39
  • @Parag The line adds a new workbook. Obviously you'd only use it if you actually *want* a new workbook. Otherwise use `Set oWBook = oExcel.Obj.Workbooks.Open("...")` to open an existing workbook. – Ansgar Wiechers Jan 19 '14 at 12:42