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 ?

Recommended Answers

Why not use a pathname like @"C:\....\Test.xls" ?

Jump to Post

Read this http://weblogs.asp.net/pwelter34/archive/2006/02/08/create-a-relative-path-code-snippet.aspx
it might help.
It is also possible that Open only accepts absolute paths.

Jump to Post

All 5 Replies

Why not use a pathname like @"C:\....\Test.xls" ?

Are you sure about the path wheather it is right or not

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.