Dismissile
Dismissile

Reputation: 33071

Excel References problem in C# code

I have a spreadsheet that is using the WORKDAY() function in Excel (which I believe lives in the Analysis Toolpak library/package/whatever you call it in Excel). When I open this spreadsheet manually and call this function it behaves fine.

I have a macro that references the cell that the WORKDAY() function is used in:

Dim startDate as Date
startDate = Cells(1,1)

When I run the macro after opening the spreadsheet manually this code executes fine. The problem is when I try and open the spreadsheet and run the macro from my C# code. It will then fail when it tries to get the value for startDate. When I look at that cell I see a #VALUE! reference error. It seems like it is not loading the correct references when I open the spreadsheet from my C# code.

Does anyone know how to resolve this issue?

Upvotes: 2

Views: 602

Answers (1)

John Alexiou
John Alexiou

Reputation: 29244

What I had to do is force the loading of the .xla add-in using:

app.Workbooks.Open(file_name)

where app is an instance of Microsoft.Office.Interop.Excel.Application if it already open then nothing happens, otherwise it loads the analysis addin. To find the path for the add-in it depends on the version of Office

  • Office 11: C:\Program Files\Microsoft Office\OFFICE11\Library\Analysis\ANALYS32.XLL
  • Office 12: C:\Program Files (x86)\Microsoft Office\Office12\Library\Analysis\ANALYS32.XLL

Upvotes: 3

Related Questions