i'm trying to open an existing excel worksheet. my code is:

Microsoft.Office.Interop.Excel.Application excelApp;
Microsoft.Office.Interop.Excel.Workbook excelWorkbook;
Microsoft.Office.Interop.Excel.Worksheet ExlWrkSheet;
private object Opt = Type.Missing

excelApp = new ApplicationClass();

excelWorkbook = excelApp.Workbooks.Open("..//..//Data.xls", Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt, Opt);

on the last line i get an exception that he cannot find the file.
i placed the file in the project directory.
i tried also "Data.exl" path when the file is placed in the debug directory, it didn't helped.

what am i doing wrong ?

i tried it with an absolute path like "c:\Data.xls" and it works fine.
the problem is with relative path.

Open does except relative paths, however, you have to be sure of where you are vs where your file is which is often unlikely.

So, if you have your app, and you are sure you are in your exe's directory, then opening "Data/file" will work - but, for example, excel may not be in the apps directory but its own, you can tell by using the filemonitor from microsoft (seeing as they bought the company out) which will show you where it was trying to find the file.

This article has been dead for over six months. Start a new discussion instead.