Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Our task is , we need to run the excel macro through programatically.

In local environment it was working fine, when we follow the below Steps:-

1) Microsoft.Office.Interop.Excel.Application appExcel = new Microsoft.Office.Interop.Excel.Application();

2) Microsoft.Office.Interop.Excel.Workbook workBook = appExcel.Workbooks.Open("FilePath",
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);

3) appExcel.Run("Macro name", "$W$5", 1, 0, "$Q$11:$Q$112", Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing,Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Note:-

On localhost everything is working fine and the macro is getting executed.

But the functionality is not working as expected when hosted in IIS(version 7.5).

For this to work the changes which we have done are:-

a) Control Panel -> Administrative Tools -> Component Services
Computers -> My Computer -> DCOM Config -> Microsoft Excel Application -> Properties

i) Here in the security tab we have Customized all the permissions by adding user(IUSR) and giving him all
access permissions.

After doing these changes we are able to create instance of Interop.Excel.
But we could not open the workbook.

ii) In Identity tab of Microsoft Excel Application peroperties, by default "The launching user" is selected.

After changing the value to "The interactive user" we are able to open the workbook.
But we could not run the macro.

iii) C:\Windows\System32\config\systemprofile

In this location we have created Desktop folder. For this folder properties, in the security tab we have created user(IUSR) and given full control.
Now we are able to run the macro and getting the desired output even when running the application on IIS hosted site.

Issue:-

After making all the changes when we have moved the changes to production server, there we could not find Microsoft Excel Application in DCOM Config.
Microsoft Office is not completely installed in production server. Only Excel software was installed on the server.

We are getting null reference exception at below line of code.

Microsoft.Office.Interop.Excel.Workbook workBook = appExcel.Workbooks.Open("FilePath",
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);

Our production server is running on windows 2008 R2.

Could any one please suggest the right way to resolve this issue.
Posted
Updated 2-Feb-16 1:34am
v2
Comments
F-ES Sitecore 2-Feb-16 7:36am    
Automating Excel via asp.net isn't supported, you're not going to get this working in a reliable manner.
Richard Deeming 2-Feb-16 8:11am    
Read the following Microsoft knowledgebase article:

Considerations for server-side Automation of Office[^]
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.

Unfortunately, there doesn't seem to be any way to execute a macro without using Office Interop. You'll need to find an alternative approach that doesn't rely on macros.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900