Watch, Follow, &
Connect with Us

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


Welcome, Guest
Guest Settings
Help

Thread: More OLE in service


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


Permlink Replies: 7 - Last Post: Jun 30, 2017 3:46 AM Last Post By: Mikael Lenfors
Mikael Lenfors

Posts: 99
Registered: 3/6/01
More OLE in service  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 27, 2017 6:06 AM
Please help!

Im running the code below as a standalone application and it works just fine!
When I run the exact same code within an Service application the "Error line" below throws exception 'Det går inte att komma åt filen D:\Services\ListServer\Document\SalesCompare.xls. Det kan finnas flera orsaker: • Filnamnet eller sökvägen finns inte. • Filen används av ett annat program. • Den arbetsbok du försöker spara har samma namn som en arbetsbok som redan är öppen'.
Translated this says something like "Can't reach file xxx, there can be different reasons, filename or path missing, file used by Another program, the file you are trying to save is already opened!'

The machine is a Windows 2012 Server R2 Standard.
I use Delphi 10.2 Tokyo.
I tried running the service with both Application.DelayInitialize as True and False, both giving exactly the same result.
I am calling "CoInitialize(Nil);" before this code and "CoUninitialize;" afterwards.
Office version is 2013, Swedish!
I use the same User account when running as a desktop application and when running as a service.

Any ideas?

Function TExcelForm.ExcelInsertText(ExcelFileName: String) : Boolean;
Var ExcelApplication : Variant;
WorkBook : Variant;
FileName: OleVariant;
UpdateLinks: OleVariant;
ReadOnly: OleVariant;
Format: OleVariant;
Password: OleVariant;
WriteResPassword: OleVariant;
IgnoreReadOnlyRecommended: OleVariant;
Origin: OleVariant;
Delimiter: OleVariant;
Editable: OleVariant;
Notify: OleVariant;
Converter: OleVariant;
AddToMru: OleVariant;
Local: OleVariant;
CorruptLoad: OleVariant;
Begin
ExcelApplication := CreateOleObject('Excel.Application');
ExcelApplication.DisplayAlerts := False;
ExcelApplication.Caption := 'ListServer';
ExcelApplication.EnableAnimations := False;
ExcelApplication.EnableAutoComplete := False;
ExcelApplication.Interactive := False;
ExcelApplication.LargeButtons := False;
While ExcelApplication.WorkBooks.Count > 0 Do ExcelApplication.WorkBooks[0].Delete;
FileName := ExcelFileName;
UpdateLinks := 0;
ReadOnly := True;
Format := 4; // Fältseparator vid öppning av fil ;
Password := '';
WriteResPassword := '';
IgnoreReadOnlyRecommended := True;
Origin := xlWindows;
Delimiter := ';';
Editable := False;
Notify := False;
Converter := EmptyParam;
AddToMru := False;
Local := False;
CorruptLoad := xlRepairFile ;
// The Open command belo throws exception !!
WorkBook := ExcelApplication.WorkBooks.Open(FileName,UpdateLinks,ReadOnly,Format,Password,WriteResPassword,IgnoreReadOnlyRecommended,Origin,Delimiter,Editable,Notify,Converter,AddToMru,Local,CorruptLoad);
// Do some stuff here
WorkBook.Close(False);
ExcelApplication := Unassigned;
End;

Robert Triest

Posts: 687
Registered: 3/24/05
Re: More OLE in service  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 28, 2017 12:50 AM   in response to: Mikael Lenfors in response to: Mikael Lenfors
Try to find out if a Windows Service has different access rights than the normal user on the same machine.
Windows services have access rights to the "Windows\system" path.
Googling "windows service file access denied" gives many hits about this problem and maybe your problem is related to this.
Mikael Lenfors

Posts: 99
Registered: 3/6/01
Re: More OLE in service  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 28, 2017 1:23 PM   in response to: Robert Triest in response to: Robert Triest
Ok thanks, but as I stated in my question "I use the same User account when running as a desktop application and when running as a service."
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: More OLE in service  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 28, 2017 1:52 PM   in response to: Mikael Lenfors in response to: Mikael Lenfors
Mikael Lenfors wrote:

Ok thanks, but as I stated in my question "I use the same User
account when running as a desktop application and when running as a
service."

Yes, but your service is still running in a different session then a
normal user would be running in. That alone can have an effect on how
things are accessed.

Do you have a similar error if you just open the XLS file directly, such as
with FileOpen() or a TFileStream? If that works, the problem has to be
in Excel itself, not in your code. Talk to Microsoft about it.

--
Remy Lebeau (TeamB)
Mikael Lenfors

Posts: 99
Registered: 3/6/01
Re: More OLE in service  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 30, 2017 2:30 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
I have rewritten my application so it's now a normal dekstop application. It works perfectly when running from the desktop!

I run the following code sequence

Var WordApplication: Variant;

WordApplication := CreateOleObject('Word.Application');
WordApplication.Caption := 'ListServer';
WordApplication.Options.CheckSpellingAsYouType := False;
WordApplication.Options.CheckGrammarAsYouType := False;
Logg(0, 'Open document count in word: ' + IntToStr(WordApplication.Documents.Count), 0);
WordApplication.Documents.OpenNoRepairDialog(FileName,
EmptyParam, // ConfirmConversions
EmptyParam, // ReadOnly
EmptyParam, // AddToRecentFiles
EmptyParam, // PasswordDocument
EmptyParam, // PasswordTemplate
EmptyParam, // Revert
EmptyParam, // WritePasswordDocument
EmptyParam, // WritePasswordTemplate
EmptyParam, // Format
EmptyParam, // Encoding
EmptyParam, // Visible
True, // OpenAndRepair
EmptyParam, // DocumentDirection
True, // NoEncodingDialog
EmptyParam); // XMLTransform
Logg(0, 'Open document count in word: ' + IntToStr(WordApplication.Documents.Count), 0);

When starting the program from the desktop everything works perfect. The log shows 0 files before the OpenNoRepairDialog command and 1 file afterwards.
When i start the program from Task Scheduler in Windows (with the same user account) it says 0 files both before and after the OpenNoRepairDialog command!?

The problem is similar in Excel. When trying to "Open" an Excelfile it works perfectly when run as a dekstop application, but when run from Task Scheduler it throws an exception.

What to do?

Best regards, Mikalel
Mikael Lenfors

Posts: 99
Registered: 3/6/01
Re: More OLE in service  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 30, 2017 2:34 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Do you have a similar error if you just open the XLS file directly, such as
with FileOpen() or a TFileStream? If that works, the problem has to be
in Excel itself, not in your code. Talk to Microsoft about it.

I tried to open and read the file with "FileOpen" as you suggested and it works just fine! That means the service has full access to the file, it's just Word and Excel that does fail to open when run as a service or from Task scheduler.

Tried mailing Bill, but no response :-)
Mikael Lenfors

Posts: 99
Registered: 3/6/01
Re: More OLE in service  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 30, 2017 3:46 AM   in response to: Mikael Lenfors in response to: Mikael Lenfors
Finally solved it :-)

Se the following link: http://troyvssharepoint.blogspot.se/2012/07/stumbled-upon-interesting-one-today.html

Seem like in >= Office 2010 you have to create the folloing folders:
C:\Windows\System32\config\ systemprofile\Desktop
and
C:\Windows\SysWOW64\config\systemprofile\Desktop

You also have to give the user running the service or the Scheduler access to the following folders:
C:\Windows\System32\config\systemprofile\Desktop
C:\Windows\System32\config\systemprofile\AppData\Roaming\Microsoft
C:\Windows\System32\config\systemprofile\AppData\Local\Microsoft
C:\Windows\SysWOW64\config\systemprofile\Desktop
C:\Windows\SysWOW64\config\systemprofile\AppData\Roaming\Microsoft
C:\Windows\SysWOW64\config\systemprofile\AppData\Local\Microsoft

Best regards, Mikael
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: More OLE in service  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 28, 2017 1:51 PM   in response to: Robert Triest in response to: Robert Triest
Robert Triest wrote:

Try to find out if a Windows Service has different access rights than
the normal user on the same machine.

Delphi services run in the SYSTEM account by default, so they have full
access to the local machine.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02