0

In my project, I need to automate Excel and Word at the server side for use by clients. I ran my code in a sample console application and all works well, but inside of the WCF service, I got some errors.

My code looks like this:

var wordApp = new Word.Application();
wordApp.Visible = true;
wordApp.Documents.Add();

wordApp.Selection.PasteSpecial(Link: true, DisplayAsIcon: true); //Throws exception

var _excelApp = new Excel.Application();
_excelApp.Visible = true;

_excelApp.Worksheets.Add(); //Throws exception

And the errors are:

System.Runtime.InteropServices.COMException was unhandled by
user code
HelpLink=wdmain11.chm#24822
Message=The specified data type is unavailable.
Source=Microsoft Word
ErrorCode=-2146822946
StackTrace:
at Microsoft.Office.Interop.Word.Selection.PasteSpecial(Object&
IconIndex, Object& Link, Object& Placement, Object& DisplayAsIcon,
Object& DataType, Object& IconFileName, Object& IconLabel)
at OfficeApiPlugin.UsingOfficeApiService.DisplyWorksheet(WorksheetRow[]
worksheetData)
at SyncInvokeDisplyWorksheet(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object
instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&
rpc)


System.Runtime.InteropServices.COMException was unhandled by user
code
Message=Exception from HRESULT: 0x800A03EC
Source=Microsoft.Office.Interop.Excel
ErrorCode=-2146827284
StackTrace:
at Microsoft.Office.Interop.Excel.ApplicationClass.get_Worksheets()
at OfficeApiPlugin.UsingOfficeApiService.DisplyWorksheet(WorksheetRow[]
worksheetData) in C:\Users\Mahdi7s\Documents\Visual Studio
2010\Projects\OfficeApiPlugin\OfficeApiPlugin\UsingOfficeApiService.cs:line
29
at SyncInvokeDisplyWorksheet(Object , Object[] , Object[] )
at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object
instance, Object[] inputs, Object[]& outputs)
at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&
rpc)

How can I do this without this errors?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mahdi7s
  • 813
  • 1
  • 9
  • 14
  • How do you host your WCF service? – Dyppl Aug 30 '11 at 09:22
  • The errors seem to indicate that the Word and Excel apps aren't installed on your server. BTW: I would always try to **avoid using** the COM interop on a server - try to use some other means (like the OpenXML interfaces) to create your Word or Excel documents on a server **without** having to install Office on your server.... – marc_s Aug 30 '11 at 09:25
  • @Dyppl:i hosted my service inside of a wpf application. – Mahdi7s Aug 30 '11 at 09:50
  • @VirtualWorld: is it on the same machine on which you ran your test console app? – Dyppl Aug 30 '11 at 09:58
  • @marc_s: The office 2010 is fully installed at the server side,"using OpenXML like" , I need to run any office app with the client request. I think I can do this only with COM ?! – Mahdi7s Aug 30 '11 at 09:58
  • @Dyppl: yes i tested the console app at server – Mahdi7s Aug 30 '11 at 10:00
  • Can you provide the part of code in which you are hosting your service? – Dyppl Aug 30 '11 at 10:08
  • @Dyppl: i used SelfDescribingServiceHost class for hosting the codes are looks like this tutorial codes:http://msdn.microsoft.com/en-us/library/aa395224.aspx - i have some other services that hosted using this class and haven't any problem, i think the problem is from office security! – Mahdi7s Aug 30 '11 at 10:26
  • @VirtualWorld: that is possible. Which binding do you use? – Dyppl Aug 30 '11 at 10:28
  • @Dyppl: first excuse me, i hadn't access to internet. my binding is WSDualHttpBinding . – Mahdi7s Sep 02 '11 at 18:19
  • See http://stackoverflow.com/questions/1031513/office-interop-with-64bit-windows-in-asp-net/1031697#1031697 – John Saunders Oct 06 '11 at 02:59

1 Answers1

0

I think the problem is that the clipboard (which is the operation that is blowing up) requires the use of an STA COM thread. WCF calls are performed on threadpool threads which are MTA threads.

Spin up your own thread using the Thread class and set its ApartmentState to ApartmentState.STA and then perform your Office automation from there. The simplest thing then would be to block your WCF thread until the automation is complete.

A couple of things to be aware of, however:

  1. If you have a lot of concurrent calls you will have to create a threadpool of STA threads rather than spin a new one up each time.
  2. For higher throughput you may want to consider making your service operation asynchronous and creating a custom IAsyncResult that signals when the automation is complete. This will allow the WCF thread to be reused while the automation is in progress.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Richard Blewett
  • 6,089
  • 1
  • 18
  • 23
  • thanks for answer, Richard i tried running my codes in another thread with STA ApartmentState, and also changed Concurrent behavior of my service from multiple to single but i still got that errors ... – Mahdi7s Sep 02 '11 at 18:23