Home » excel » delphi – Exporting Excel Inside Service

delphi – Exporting Excel Inside Service

Posted by: admin May 14, 2020 Leave a comment

Questions:

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!

How to&Answers:

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!

Answer:

You are not specifying a full path and so will be attempting to save the file to whatever happens to be the working directory of your service. It appears that the working directory for your service is a directory that does not exist.

The appropriate solution here is to provide a full path to the file. Since your goal here is to save a file temporarily so that you can then attach it to an e-mail, it makes sense to save to the temporary directory. Find the temporary directory by calling GetTempPath.