Home > database >  Why does using this closing line of code does not work?
Why does using this closing line of code does not work?

Time:06-09

The code stops at the following line working because of the following line

objExcel.Workbooks("cde.XLSX").close

The code properly downloads data from SAP and save it as "cde.xlsx" in a folder location. Once the saving is processed the file opens up by itself and the code is supposed to close it. The closing step does not to work. It used to work, did not change anything in the code.

Dim objExcel
Dim objSheet, lastrowRow, i
Set objExcel = GetObject(,"Excel.Application")
Set objSheet = objExcel.Workbooks("Extract_zrep.xlsm").Sheets("Sheet1")
Set lien = objExcel.Workbooks("Extract_zrep.xlsm")
lastrow = objSheet.Range("B65536").End(-4162).Row

SaveToFolder = objSheet.Cells(5, 5).Value
Date1 = Trim(CStr(objSheet.Cells(2, 6).Value))
Date2 = Trim(CStr(objSheet.Cells(2, 8).Value))
    session.findById("wnd[0]/tbar[0]/okcd").text = "/nZMM_xxxx"
    session.findById("wnd[0]").sendVKey 0
    session.findById("wnd[0]/tbar[1]/btn[17]").press
    session.findById("wnd[1]/usr/txtV-LOW").text = ""
    session.findById("wnd[1]/usr/txtENAME-LOW").text = "USER"
    session.findById("wnd[1]/tbar[0]/btn[8]").press
    session.findById("wnd[0]/usr/ctxtP_VARI").text = "CDE"
    session.findById("wnd[0]/tbar[1]/btn[8]").press
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").contextMenu
    session.findById("wnd[0]/usr/cntlGRID1/shellcont/shell").selectContextMenuItem "&XXL"
    session.findById("wnd[1]/tbar[0]/btn[0]").press
    session.findById("wnd[1]/usr/ctxtDY_PATH").text = SaveToFolder
    session.findById("wnd[1]/usr/ctxtDY_FILENAME").text = "cde.XLSX"
    session.findById("wnd[1]/usr/ctxtDY_FILENAME").caretPosition = 3
    session.findById("wnd[1]/tbar[0]/btn[0]").press
objExcel.Workbooks("cde.XLSX").close

enter image description here

CodePudding user response:

Edited:

If you know the workbook in discussion full name, as I suppose, please replace that not working code line with the next code:

  Dim objEx As Object, wbFullName As String
   
  wbFullName "C:\your real path\cde.XLSX" 'please, use the real folder path!
  Set objEx = GetObject(wbFullName).Application
  objEx.Workbooks("cde.XLSX").close
  objEx.Quit

It will find and close the new Excel Session, where the workbook is open, close the workbook and quit session...

  • Related