I have written a Windows Service in C#. My service is meant to open an Excel macro-enabled workbook (this is in Excel 2010). I have installed this service on our server which is running Windows Server 2008 64-bit. My service seems to have a problem launching Excel when no one is logged on the server, does anyone have a solution to this?
I get the following error:
System.Runtime.InteropServices.COMException (0x8000401A): Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 8000401a.
Excel is installed on the server, the service works fine when I am logged on the server but once all users have logged off, I get the above error. I would like the service to launch my Excel workbook regardless of an open session on the server or not.
My service seems to have a problem launching Excel when no one is logged on the server, does anyone have a solution to this.
Of course it has a problem. Windows Services cannot show a user interface, so how would you expect it to launch a GUI application like Microsoft Excel when there is no user logged in?
The specific COM error code that you receive means:
8000401a: The server process could not be started because the configured identity is incorrect. Check the username and password.
In other words, Excel is trying to start as an interactive user, which refers to the user that is currently logged on directly to the server console. Since no user is logged on, no interactive user exists, and the application fails when it tries to assume this identity.
The design was broken anyway: Excel was not designed to be run from a Windows Service. Create a standard Windows application instead. If you need it to run in the background without a UI of its own, don't create a window.
Basically, Cody Gray's answer is right. In the past I had a need to launch Excel from a Windows Service in order to print the file.
We were able to get past that kind of errors by setting the service to run as a specific user account and from time to time, log on as that user account and try to launch the files that failed to see the error messages popped up by Excel.
In your case, it is possibly because Excel is started for the first time and it asks you for something like your initials.
Are you using Office Automation to start Excel? Sorry, but Office Automation is not supported in service processes. It is designed to work only in an interactive process.
If you're lucky, Using Office Automation from a service process won't work. If you're not lucky, it will appear to work, and you'll actually put your application into production. You'll then start finding random bugs which are very difficult to reproduce, and even more difficult to fix without breaking something else.
That will be due to the fact that the real bug is a design bug - you used Office Automation from a service process.
Take it from the Voice of Experience...
Related
I'm writing a program that needs to open up Microsoft Outlook and create a mail item for the user when they click on a button. However, when I do so, I get the following error:
Retrieving the COM class factory for component with CLSID {0006F03A-0000-0000-C000- 000000000046} failed due to the following error: 80080005 Server execution failed (Exception from HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
After researching this error I found out that my program and MS Outlook must both be run as administrator or as normal privilege level.
So here's my question... How do I open MS Outlook through the C# code by using the same privilege level as my current running program. I need to get the current privilege level, then open Outlook with that privilege level. I've had no luck in my research with this so far. Any help is appreciated!
Here's my code (currently) for opening MS Outlook and how I use it:
Application outlookApp = new Application();
MailItem mailItem = outlookApp.CreateItem(OlItemType.olMailItem);
mailItem.Subject = "Blah";
mailItem.HTMLBody= #"Various HTML stuff";
foreach (string documentPath in this.documentPaths)
{
mailItem.Attachments.Add(documentPath, 1, 1, documentPath);
}
mailItem.Display(true);
I am using Microsoft.Office.Interop.Outlook.
If outlook is already running with administrator. Then I don't get any error.
Please suggest.
I am using SimpleMAPI.NET to open the standard email application and send an email. I have even tuned it to open a specific email application, different from the one configured in windows. Works great with MS Outlook so far and can even be used with other email software.
SimpleMAPI.NET can be obtained from here:
http://www.codeproject.com/Articles/2048/Simple-MAPI-NET
If you deem this approach workable for you, just leave a comment and I will extend my answer by some code examples and my modifications to SimpleMAPI.NET regarding Outlook (there are some issues with the original version above).
I am proposing this here, because my team initially was using Outlook via COM (as you do, at the moment), faced several issues with this and then opted for my MAPI solution. It has proven to be much more stable an versatile, if one "only" wants to send an email via an external application...
Outlook is a singleton, so to make sure it is running on the same trust level as your app, your app must be the one that starts it.
You can try to kill Outlook first (not very nice and the user won't appreciate it) before restarting it from your app.
You can use Extended MAPI (C++ or Delphi) or Redemption (I am its author - it wraps Extended MAPI and can be used from any language) - MAPI is loaded in-proc, so it will work even if Outlook is already running. But you will not be able to display the items: outlook.exe is the one that ends up displaying messages even if you use MAPI, so you are back where you started.
Why do your app and Outlook end up running in different security contexts?
I have a weird problem, i have an silverlight application which uses Interop to execute a piece of code .
My Production server has a complex login procedure and the password never stays the same for a user after a stipulated time. i.e the password keeps changing.
So when i run this application , until any user is logged in the application works well, as soon as the person logs out or the session expires , the interop stops working as it doesn't have an interactive user.
I have read in several posts of this issue and majority like this asks to configure identity in the DCOMCNFG settings. But i am unable to find any microsoft office component (powerpoint,onenote,word,excel...) in my DCOMCNFG but in my local i am able to find it. Also the Interop is not found. The error is referring to this CLSID {00024500-0000-0000-C000-000000000046} .
How can i solve this issues. Is there any problem with the Office installation so as the file should appear in my DCOMCNFG ?
even if it does appear is there a ray of hope through which i can solve the identity problem ? as the password of my production server keeps changing so even if i go to the dcom component and go to identities tab in the properties , will i be able to give "this user" a fixed name and password ? or should i use the launching or interactive user ?
any help would be appreciated.
Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.
If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. Or, you should try to find alternatives that allow at least part of the code to run client-side. If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. Additionally, you will be taking risks with the stability of your overall solution.
You can read more about that in the Considerations for server-side Automation of Office article.
I created a Windows application which starts a Word process (Microsoft.Office.Interopt.Word) with C#. I load two addins word.AddIns.Add(#"C:\temp\addin.dot", true);. This code works perfectly fine within my windows application.
To be able to start this process remotly, I created a MVC web application. The web app uses the same code, but it failes on the part where I load the addins:
error: Word cannot open this document template: (C:\temp\addin.dot)
The web application uses an application pool with administrator permissions and following settings:
Enable 32-Bit Applications: True
Load User Profile: True
Does anyone have an idea what the problem could be? Why does the same code behave differently on the web server?
Edit: The task manager shows me that the word process gets started with the correct user account. It is the same account as I use, when I start the windows application.
Web servers aren't meant to run user applications. Since you're using a COM reference, the library requires the application to be running to perform manipulations.
If you're looking to automate document generation/reading, my advise would be to use a library like DocX. This would enable you to work directly with the file format itself without bringing in COM references (the interop libraries).
I'm attempting to use office interop to interact with Excel on a remote machine. The simplest-seeming way I found to do this was to use the Microsoft.VisualBasic.Interaction.CreateObject method, which is documented here. In my case, the call made to it is:
var excelApp = (Application)Interaction.CreateObject("Excel.Application", machineName);
However, this isn't fully working as expected. The results I'm getting:
When the machineName is the local machine that I'm running the code on, this works as expected.
When the machineName is a remote machine, an instance of Excel actually is started on the machine, but the CreateObject call fails with exception:
System.Exception: The remote server machine does not exist or is unavailable.
Googling this message doesn't reveal much of use, the results seem to all be people with different symptoms.
I don't really have any leads on what's causing this. I noticed that the method documentation page I linked previously rather cryptically states:
Refer to COM documentation (see Microsoft Developer Network) for additional information on making an application accessible on a remote networked computer. You may need to add a registry key for your application.
But I've yet to find that documentation.
Any suggestions for what could be the problem here? Or, failing that, a different way of achieving the same thing? Due to my situation, remote interop like this is a firm requirement- using Excel on a local machine to access a remote file is not sufficient.
Additionally, I would very much like to avoid a solution that requires a service or anything similar to be running on the Excel client machine, which as I understand it would be necessary if I wanted to use Activator.CreateInstance with a UrlAttribute.
The problem was a DCOM configuration issue. All the appropriate permissions were set, but the application was not configured to run as the interactive user. I suspect that this problem is relatively uniquely identified by the combination of being able to launch the process but not being able to connect to it afterwards.
The setting for this can be found in Component Services (dcomcnfg), under:
Component Services > Computers > My Computer > DCOM Config > Microsoft Excel Application. Right click this, select 'Properties', and configuration for which user account to run under is found on the 'Identity' tab.
An additional problem that I found along the way was that 'Microsoft Excel Application' was not initially listed in Component services, due to having 32-bit Excel on 64-bit Windows.
A solution for this, found here, was to run the 32-bit version of mmc (which can be done from the command line by starting with argument -32) and use the Component Services snap-in
I am experiencing an issue with an application that is attempting to access Quickbooks in blind mode (i.e. Quickbooks is not running). In short, I have a simple C# application that synchronizes information between a Quickbooks company file and SQL database on a web server to allow customers to make payments toward invoices online. The UI for the application only has one button and a few other settings so, I modified the code so that it can be run as a scheduled task in the middle of the night. Unfortunately, this has been much more trouble than I anticipated.
First, I could not get it to run through the task scheduler or an elevated command prompt. Then, I came across a post that stated that there is a bug in the SDK when attempting to access Quickbooks via the QBSDK if the host is a terminal server with multiple sessions. The temporary solution was to make sure that Quickbook is running on the session that the job is running on to prevent it from trying to attach to another session. So, I made sure that Quickbooks is loaded but, NOT logged in to a company file. While this solution works if I try to run my application from an elevated command prompt it still does not work when the job is launched from the windows task scheduler. I am not logged into the server remotely now but, the error message states something about not being able to open a session because there is a modal dialog box open. I will modify the post to include the specific error message as soon as I am able to connect to my client's server. Quickbooks is open on the session that is trying to run as a scheduled task and I am able to run the application from an elevated command prompt. Can someone please offer any suggestions or feedback about how I might go about solving this problem? Thanks in advance for your help!