1

I am trying to save my Excel file that I created in c#. I added a DateTime.Now to the title and made sure the characters where compatible. Now I have the above error when the folder exists. I have posted what it says could be the error but none apply. Also when I get the error it tells me the DateTime.Now portion is say for todays date and time started was 5/9/8E017700. That is not not a date or time for year and time. I think that maybe the problem but I am unsure as to why it is altered when I never change the variable for name. How can I get this problem resolved?

• The file name or path does not exist.

• The file is being used by another program.

• The workbook you are trying to save has the same name as a currently open workbook.

string date = DateTime.Now.ToString().Replace(":", ".");

string filename = "IncomingProduct-" + date + ".xls";

string subPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "/Documents/Reports/";

    bool isExists = Directory.Exists(subPath);

    if (!isExists)
        Directory.CreateDirectory(subPath);

    xlWorkBook.SaveAs(subPath + filename);//error here
    xlWorkBook.Close(true, misValue, misValue);
    xlApp.Quit();

    System.Diagnostics.Process.Start(subPath + filename);
4
  • does "/Documents/Reports/" exist already? Should it be "//Documents//Reports//" in this case? Commented May 9, 2014 at 19:27
  • @N4TKD yes it does if it doesnt it is created. Commented May 9, 2014 at 19:29
  • Set a breakpoint on the xlWorkBook.SaveAs line, and examine the contents of subPath and fileName. What exactly are the values of those variables when your code is executed? Commented May 9, 2014 at 19:31
  • I would use string path = Path.combine(subPath, filename); and debug and make sure the result is what you need. Commented May 9, 2014 at 19:32

1 Answer 1

2

You cannot use file names with slashes so if the date has slashes it won't save it.

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

1 Comment

Thank you soooo much for your help, I was searching all the internet and try to grand access to all things that I found there. At the end this was the fix!

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.