Close and Re-Open Excel Application

Hi,

I have to write a method that Save, Close and Re-Open an Excel Application.

The attached code have two bugs:

  1. When i try to save the workbook it appear a window to confirm the saving (i don't want it)
  2. when i make the quit of the application if doens't really close the Excel Application, so when i restart the application it create a second instance of Excel

p.s.

i am sorry i don't find the tag to write the code in the message


bungle77 - Mon May 20 11:13:22 EDT 2013

Re: Close and Re-Open Excel Application
llandale - Mon May 20 13:51:18 EDT 2013

Don't know about Excel, but this works for Word:

  •        put(oaaArgs, "NoPrompt",       true)

-Louie

Re: Close and Re-Open Excel Application
bungle77 - Tue May 21 05:09:40 EDT 2013

llandale - Mon May 20 13:51:18 EDT 2013

Don't know about Excel, but this works for Word:

  •        put(oaaArgs, "NoPrompt",       true)

-Louie

it doesn't work

i was able to fix the first problem like this

 

    OleAutoObj objExcelWorkbooks = null
    OleAutoObj objExcelWorkbook = null
    int i, i_sheets
    oleGet(oleExcel,"Workbooks", objExcelWorkbooks)     
    oleGet(objExcelWorkbooks,"Count", i_sheets)

    for (i=1;i<i_sheets+1;i++) {
        clear autoArgs
        put(autoArgs ,i)
        oleGet(oleExcel,"Workbooks",autoArgs ,objExcelWorkbook)
        put(autoArgs ,"SaveChanges",true)    
        oleMethod(objExcelWorkbook, "Close",autoArgs)
    }
    delete autoArgs

    
    oleCloseAutoObject(objExcelWorkbook)
    olePut(oleExcel,"DisplayAlerts ",false)
    oleMethod(oleExcel, "Quit")
    oleCloseAutoObject(oleExcel)

 

i still have the problem i can't kill the Excel application


Attachments

provaExcel.dxl

Re: Close and Re-Open Excel Application
llandale - Tue May 21 10:59:28 EDT 2013

bungle77 - Tue May 21 05:09:40 EDT 2013

it doesn't work

i was able to fix the first problem like this

 

    OleAutoObj objExcelWorkbooks = null
    OleAutoObj objExcelWorkbook = null
    int i, i_sheets
    oleGet(oleExcel,"Workbooks", objExcelWorkbooks)     
    oleGet(objExcelWorkbooks,"Count", i_sheets)

    for (i=1;i<i_sheets+1;i++) {
        clear autoArgs
        put(autoArgs ,i)
        oleGet(oleExcel,"Workbooks",autoArgs ,objExcelWorkbook)
        put(autoArgs ,"SaveChanges",true)    
        oleMethod(objExcelWorkbook, "Close",autoArgs)
    }
    delete autoArgs

    
    oleCloseAutoObject(objExcelWorkbook)
    olePut(oleExcel,"DisplayAlerts ",false)
    oleMethod(oleExcel, "Quit")
    oleCloseAutoObject(oleExcel)

 

i still have the problem i can't kill the Excel application

Your code seems to work.  When there is no "Excel.exe" running (as per task Manager), the code ends up with exactly one "excel.exe" running and one workbook displayed.  I notice that temporarily there are two "excel.exe" displayed, but that is a feature of taskmanager update rate.

I notice that when you run the code twice; the 1st time you get a new sheet "aaa", the 2nd time a new sheet "Sheet4", which cannot be renamed becase "aaa" already exists.

Your "Workbooks" loop starting at line #70 doesn't feel right; you can save and close individual worksheets in excel?

I think you are getting errors and don't know it.  I suggest you add this function:

  • bool CheckRes(string Message)
  • {  if (!null Message)
  •    { print Message "\n"
  •      return(false)
  •    }
  •    else return(true)
  • }

Then surround your "oleMethod"/"oleGet"/"olePut" calls with the call:

  • CheckRes(oleMethod(objExcelWorkbook, "Close", autoArgs))

-Louie