Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Delphi XE7. Windows service with Ole Automation Excel. OLE error 800A03EC


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 1 - Last Post: Jul 3, 2017 6:39 AM Last Post By: Pablo Romero
Pablo Romero

Posts: 21
Registered: 9/28/00
Delphi XE7. Windows service with Ole Automation Excel. OLE error 800A03EC  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 3, 2017 5:05 AM
Hi Fellas.

I have built an Service App in Delphi, which should generate an MS Excel
workbook with seven worksheets and upload it to an FTP server, a few
times per day.

I programmed the service in a dual way: application and service. It
works fine. But...

When I use the service "as an application" inside the IDE to debug it,
it works like a charm.

When I use it as a Windows service, it works ok, but every time that
tries to save the workbook generated it gives the "OLE error 800A03EC".

The code is like this

   Excel := TExcelApplication.Create(nil);
   Hoja  := TExcelWorkSheet.Create(nil);
   try
      try
         nro := GetUserDefaultLCID;
         Excel.AutoConnect := false;
         Excel.ConnectKind := ckNewInstance;
         Excel.Connect;
         Excel.DisplayAlerts[nro] := False;
         Excel.ScreenUpdating[nro] := false;
         Excel.Visible[nro] := false;
 
         Excel.Workbooks.Add(TOleEnum(xlWBATWorksheet), nro);
         Excel.Workbooks[1].Sheets.Add(EmptyParam, EmptyParam, 6, xlWorksheet, nro);
 
         if terminated then
            exit;
 
         for LaHoja := Low(TExportacion) to High(TExportacion) do
         begin
             Hoja.ConnectTo( Excel.Worksheets[ord(LaHoja)] as _Worksheet );
             Hoja.Name := aExportaciones[LaHoja].NombreTabla;
 
             if terminated then
                break;
 
             ProcessExportToExcel(LaHoja);
 
             if terminated then
                break;
         end;
 
         Nombre := 'C:\Users\Public\Documents\Exportaciones\LAYOUT_ARGENTINA Junio-2017.XLS';
 
         try
            if not terminated then
            begin
               Excel.Workbooks[1].SaveAs(Nombre, EmptyParam, EmptyParam,
                                         EmptyParam, EmptyParam,
                                         EmptyParam, xlNoChange,
                                         EmptyParam, EmptyParam,
                                         EmptyParam, EmptyParam, EmptyParam, nro);  <<--- Here, it gives the error
 
            end;
         except
           on e:exception do
             Log( error...);
         end;
         Excel.Workbooks.Close(nro);
 
      except
         on e:exception do
            Log( ... );
      end;
 
   finally
       if Assigned(Excel) then
       begin
          Hoja.Disconnect;
          Hoja.Free;
 
          Excel.Quit;
          Excel.Disconnect;
          Excel.Free;
       end;
   end;
end;


Any ideas?

Thank you in advance

Pablo Romero
Cordoba, Argentina
Pablo Romero

Posts: 21
Registered: 9/28/00
Re: Delphi XE7. Windows service with Ole Automation Excel. OLE error 800A03EC [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 3, 2017 6:39 AM   in response to: Pablo Romero in response to: Pablo Romero
Ok, I solved this problem by myself. After tons of tests, here I share
the solution.

If your customer has MS Office 2007, 2010 or 2013

* Go to the Excel Trust Center and turn off all options. Trust everything

* Do not generate XLS file. use XLSX files.

* Do not use

   Excel.Workbooks[1].SaveAs(....)


* Do not use the "Users..." path. Create some folder with all atributes
for the user. Although Windows Services run under the SYSTEM account and
this account is allowed to write on all your hard drive.

* I did that, like this

   Nombre := 'C:\MyOtherNewPath\MyFile.XLSX'
   Excel.Workbooks[1].SaveCopyAs(Nombre, nroLcid )


I think Delphi urgently needs some API to generate ODS files, so we do
not need to have installed MS Excel or OpenOffice or LibreOffice any more.


Regards
Pablo Romero
Cordoba, Argentina


El 03/07/2017 a las 9:10, Pablo Romero escribió:

Hi Fellas.

I have built an Service App in Delphi, which should generate an MS Excel
workbook with seven worksheets and upload it to an FTP server, a few
times per day.

I programmed the service in a dual way: application and service. It
works fine. But...

When I use the service "as an application" inside the IDE to debug it,
it works like a charm.

When I use it as a Windows service, it works ok, but every time that
tries to save the workbook generated it gives the "OLE error 800A03EC".

The code is like this

    Excel := TExcelApplication.Create(nil);
    Hoja  := TExcelWorkSheet.Create(nil);
    try
       try
          nro := GetUserDefaultLCID;
          Excel.AutoConnect := false;
          Excel.ConnectKind := ckNewInstance;
          Excel.Connect;
          Excel.DisplayAlerts[nro] := False;
          Excel.ScreenUpdating[nro] := false;
          Excel.Visible[nro] := false;
 
          Excel.Workbooks.Add(TOleEnum(xlWBATWorksheet), nro);
          Excel.Workbooks[1].Sheets.Add(EmptyParam, EmptyParam, 6, xlWorksheet, nro);
 
          if terminated then
             exit;
 
          for LaHoja := Low(TExportacion) to High(TExportacion) do
          begin
              Hoja.ConnectTo( Excel.Worksheets[ord(LaHoja)] as _Worksheet );
              Hoja.Name := aExportaciones[LaHoja].NombreTabla;
 
              if terminated then
                 break;
 
              ProcessExportToExcel(LaHoja);
 
              if terminated then
                 break;
          end;
 
          Nombre := 'C:\Users\Public\Documents\Exportaciones\LAYOUT_ARGENTINA Junio-2017.XLS';
 
          try
             if not terminated then
             begin
                Excel.Workbooks[1].SaveAs(Nombre, EmptyParam, EmptyParam,
                                          EmptyParam, EmptyParam,
                                          EmptyParam, xlNoChange,
                                          EmptyParam, EmptyParam,
                                          EmptyParam, EmptyParam, EmptyParam, nro);  <<--- Here, it gives the error
 
             end;
          except
            on e:exception do
              Log( error...);
          end;
          Excel.Workbooks.Close(nro);
 
       except
          on e:exception do
             Log( ... );
       end;
 
    finally
        if Assigned(Excel) then
        begin
           Hoja.Disconnect;
           Hoja.Free;
 
           Excel.Quit;
           Excel.Disconnect;
           Excel.Free;
        end;
    end;
end;


Any ideas?

Thank you in advance

Pablo Romero
Cordoba, Argentina
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02