Excel automation in console application

I’ve got a console app that get some data from a db, put them into an existent excel, then send the filled excel via email. The app works fine if I manually launch it from prompt, but if I schedule a task that launch the app, the app start, I can see excel in task manager, but all seems to be hang and the app doesn’t send email.

This is the code part using excel automation.

Var excel As New ExcelApplication
excel.Workbooks.Open(templateXls.NativePath)

var row as Integer = 2
var cell as String
While not r.AfterLastRow
  
  For c as Integer = 0 to r.ColumnCount-1
    cell = chr(65+c)+row.ToText
    if r.ColumnAt(c).Name = "LDAINF" or r.ColumnAt(c).Name = "LDFATT" or r.ColumnAt(c).Name = "LDAFIF" then
      excel.Range(cell).Value = dateAs2ggmmyyyyy(r.ColumnAt(c).Int64Value)
    else
      excel.Range(cell).Value = r.ColumnAt(c).StringValue
    end If
    
  next
  
  row = row + 1
  r.MoveToNextRow
wend

excel.ActiveWorkbook.SaveAs(output.NativePath)
Excel.Quit
Excel = Nil

Any ideas?

This might help … some software requires an active session. In the task scheduler, use the “Run only when user is logged on” option. (This is in the General tab in the properties of your task.) Note this means that you must leave the computer logged in when the task needs to run.

Thanks Eric!

You’re right. Using task scheduler with “Run only when user is logged on” option, the program works fine but… I really don’t like to have my app that depends from an active session on the server.
I’d like to find an alternative solution that not depends from active session.