2

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?

3
  • Could it be a problem with macros being disabled in general when opening through C#? Can you confirm that you can run any macro at all? Commented Sep 15, 2011 at 14:18
  • The macro runs. It stops halfway through the macro on the line that tries to set the date. Commented Sep 15, 2011 at 14:28
  • If you open Excel via automation it doesn't automatically loads add-ins: you need to do that in your code. support.microsoft.com/kb/213489 Commented Sep 15, 2011 at 18:03

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.