Search code examples
exceldelphiserviceexport-to-excel

Exporting Excel Inside Service


Good Evening!

I have built an Service App in Delphi, which should generate an excel and send it by email a few times per day.

Basically, I have my query components well configured (SQLQuery, Provider, Client and DataSource), and all the process work properly when I'm debugging the service in the IDE.

The problem is when I try to install my service and run it without Delphi, the program runs until this line:

Excel.Application.Workbooks[1].SaveAs(pFileName + '.xls');

EDIT: In the case above, the pFileName variable was provided with 'C:\', which means I'm giving the full path.

I've got the Exception, and here is the message:

SaveAs method of Workbook class failed

This makes me think the problem is not in the code, but something in my computer (some kind of configuration).

I've configured the service with my user, so I have all permissions, also I could generate txt files within the service, but not the xls (excel) file.

Thanks very much!


Solution

  • I found the answer while I was typing this question. Even though I'm using Windows 7, this worked for me:

    When using Windows 2008 server x64, create the following directory to enable Excel importing and exporting:

    \Windows\SysWOW64\config\systemprofile\Desktop

    When using Windows 2008 server x86, create the following directory to enable Excel importing and exporting:

    \Windows\System32\config\systemprofile\Desktop

    Source: Export To Excel error - Oracle Blog

    Note: I even deleted the folder and tried again to be sure this was the solution. Thanks!