locked
file.copy of excel is also opening the file RRS feed

  • Question

  • I have code in VS2013 that looks like this:

    

    sWorkFile = mWorkDirectory + "Template_" + DateTime.Now.ToString("yyyyMMdd_HHmmssfff") + ".xls"; mLog.writeLog("template file " + mTemplateFile); System.IO.File.Copy(mTemplateFile, sWorkFile,true); mLog.writeLog("template copied"); mLog.writeLog("going to open the file now"); Excel.Workbook xlBook = xlApp.Workbooks.Open(sWorkFile, 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); //The template will be workbook number 1 mLog.writeLog("workbook opened");

    this works just fine on my 2008 32bit server but when I try this on my 32bit VM I get and the following error: Microsoft Office Excel cannot access the file. There are several possible reasons: 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.        The file is there, I can see it, no other program is using this file - nothing else is running and when I look at the task manager while debugging, I can see that s soon as I copy the file it is opened! I have no object associated with it yet, so I can't close it. As I said this code works fine on my physical 2008 32bit server and on my 2008 64bit VM it is only on my 2008 32bit VM that it happens. HELP  PLease!!!

    Wednesday, October 12, 2016 12:13 PM

Answers

  • Hi MarathonGirl,

    Thank you for posting here.

    For your question, I make a simple example with your code to reproduce your issue.

    >>file.copy of excel is also opening the file

    Excel.Workbook xlBook = xlApp.Workbooks.Open(sWorkFile, 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); 
     //The template will be workbook number 1            
    

    This method would open the excel. You could see it in Task Manager. Not in Process but in details.

    When you run the program without close the process, I get the exception.

    You could use the follow code to quit the excel.

     xlApp.Quit();

    I hope this would be helpful to you.

    Best Regards,

    Wendy


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, October 13, 2016 11:25 AM

All replies

  • File.Copy doesn't open a file. Under the hood it simply uses MoveFileEx to copy the file. No handles are needed for this.

    It sort of sounds like something else in the system is grabbing, perhaps AV or one of the services to support the VM. Use Process Explorer or similar to identify the process that has opened the file. This should help narrow down the issue.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Wednesday, October 12, 2016 1:58 PM
  • Hi MarathonGirl,

    Thank you for posting here.

    For your question, I make a simple example with your code to reproduce your issue.

    >>file.copy of excel is also opening the file

    Excel.Workbook xlBook = xlApp.Workbooks.Open(sWorkFile, 0, false, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false); 
     //The template will be workbook number 1            
    

    This method would open the excel. You could see it in Task Manager. Not in Process but in details.

    When you run the program without close the process, I get the exception.

    You could use the follow code to quit the excel.

     xlApp.Quit();

    I hope this would be helpful to you.

    Best Regards,

    Wendy


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, October 13, 2016 11:25 AM
  • Hi MarathonGirl,

    Have your problem been solved?

    If it solved, please mark the useful reply as answer.

    This will make answer searching easier in the forum and be beneficial to community members as well.

    If you have something else, please feel free to contact us.

    Best Regards,

    Wendy


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, October 20, 2016 3:39 PM