0

I have an excel file in my project which is listed as resource.

Now I try to open it with a button click like this:

private void Button_Click_Blist(object sender, RoutedEventArgs e)
{
    Excel.Application xl = new Excel.Application();
    Excel.Workbook wb = xl.Workbooks.Open("Blist.xlsx");
}

My problem is that it says that it can't find the file and throws this exception:

System.Runtime.InteropServices.COMException

We couldn't find 'Blist.xlsx'. Was the object perhaps moved, renamed, or deleted?

10
  • Does the file actually exist in the same directory as your executable? Commented Dec 2, 2019 at 9:24
  • Please provide more information about this. Something like Stacktrace, error message and so one. Commented Dec 2, 2019 at 9:24
  • @Frauke No it is not in the same directory as the executable. Commented Dec 2, 2019 at 9:29
  • @TemaTre "We couldn't find 'Blist.xlsx'. Was the object perhaps moved, renamed, or deleted?" Commented Dec 2, 2019 at 9:29
  • 1
    If the file is part of the Visual Studio project, set its Build Action to Content, and Copy to Output Directory to Copy always or Copy if newer. Commented Dec 2, 2019 at 9:35

2 Answers 2

2

This code uses OLE Automation to start Excel and tell it to open a file in the relative path Blist.xlsx. The executable in this case is Excel, not your own application. The relative path will be resolved using Excel's working directory.

To avoid this problem, pass the absolute file path to Excel :

var fullPath=Path.GetFullPath("Blist.xlsx");
Excel.Workbook wb = xl.Workbooks.Open(fullPath);

Another possibility, which gives no control of (or dependency on) Excel, is to just "start" the file with Process.Start, eg :

Process.Start("Blist.xlsx");

Or

var fullPath=Path.GetFullPath("Blist.xlsx");
Process.Start(fullPath);

The Windows Shell will find the application that can open this document based on its extension and start it, passing the file path as an argument. This can start Excel, Libre Calc or any other application registered to open this particular file extension.

Sign up to request clarification or add additional context in comments.

Comments

0

So I tried this and apparently the .Open() method some extra complexety with relative paths (see @PanagiotisKanavos comment). You can make a work around, by getting the current directory path, and pre-pending it, giving the absolute path of the file:

string filename = "Blist.xlsx";
string currentPath = Directory.GetCurrentDirectory();

var xl = new Microsoft.Office.Interop.Excel.Application();            
var wb = xl.Workbooks.Open(currentPath + "\\" + filename);

xl.Visible = true;

However, you should (almost) never use hardcoded file names in your code. So the above solution is just an example, not something to copy into production code...

2 Comments

Open works just fine with relative paths. The real question is relative to what? The application is Excel, and the working directory is Excel's working directory
@PanagiotisKanavos you are correct. I have changed the wording a bit, to reflect your comment.

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.