4

I have the this code that will create excel file and work sheet then insert same values.

The problem I'm facing that I'm not able to save the file with name giving ten colse it.

I used SaveAs but did not work:

wb.SaveAs(@"C:\mymytest.xlsx", missing, missing, missing, missing,
 missing, XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);

this line of code would give me this error:

Microsoft Office Excel cannot access the file 'C:\A3195000'. 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.

please advice to solve this problem.

here is my code:

private void button1_Click(object sender, EventArgs e)
{
 Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
 if (xlApp == null)
 {
 MessageBox.Show("EXCEL could not be started. Check that your office installation and project references are correct.");
 return;
 }
 xlApp.Visible = true;
 Workbook wb = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
 Worksheet ws = (Worksheet)wb.Worksheets[1];
 if (ws == null)
 {
 MessageBox.Show("Worksheet could not be created. Check that your office installation and project references are correct.");
 }
 // Select the Excel cells, in the range c1 to c7 in the worksheet.
 Range aRange = ws.get_Range("C1", "C7");
 if (aRange == null)
 {
 MessageBox.Show("Could not get a range. Check to be sure you have the correct versions of the office DLLs.");
 }
 // Fill the cells in the C1 to C7 range of the worksheet with the number 6.
 Object[] args = new Object[1];
 args[0] = 6;
 aRange.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, aRange, args);
 // Change the cells in the C1 to C7 range of the worksheet to the number 8.
 aRange.Value2 = 8;
 // object missing = Type.Missing;
 // wb.SaveAs(@"C:\mymytest.xlsx", missing, missing, missing, missing,
 //missing, XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, //missing);
}
SLaks
891k182 gold badges1.9k silver badges2k bronze badges
asked Apr 18, 2010 at 17:37
3
  • Why are you using Type.InvokeMember() to set cell values? Commented Apr 18, 2010 at 17:46
  • I got this example from MSDN site so I'm trying to use it and is using Type.InvokeMember() would cause problem to save the file? Commented Apr 18, 2010 at 18:29
  • No, it just seems like a strange way to do it. Commented Apr 19, 2010 at 16:23

2 Answers 2

1

Do you actually have permission to save files to the root of C? I think by default you don't have that in Windows 7.

You can try it by just trying to save an Excel file manually to C:.

answered Apr 18, 2010 at 17:47

4 Comments

My user account is part of the Admin group on my Win7 machine but I just tried and I'm not allowed to save files to the root of C. However, either way I'd suggest trying to save the file with a slightly different name to a different directory just to make sure that there's not a conflict with a previous time you ran this code. I'd also use the task manager to check if there are any old open instances of Excel that might have locked the file name in some way.
Thank you ho for your help. your advice worked for me but what I should do to be able to save in C:\ drive?
I assume that it should work to just change the security permissions. So in Windows Explorer, right click on the C: drive and select properties and then select the Security tab and then click the Edit button. Either give write/modify permissions to the Users group or add your own account to that list and give that account write/modify permissions.
Though I'd suggest that it would be better to add a directory to C: and write your files in there instead (for example a C:\Test directory). By default you should be able to write files to that directory
0

Check following:

  • use "C:\\mymytest.xlsx" as file name (no @ sign and double backslash as backslash is escape character)
  • check that you don't have another copy of Excel running - see number of EXCEL processes in task manager. It could be that you don't correctly shut down Excel and then when you create new workbook you could start new instance.
    You could check if this is a problem by making sure that you always have different file name (at least for test).
answered Apr 18, 2010 at 17:58

3 Comments

I agree with your second suggestion but not the first one, the @ sign means that it's fine with single backslashes, so removing the @ sign and adding an extra backslash should make no difference.
Hi zendar, I tried you advices but they did not work. I changed the the path and all excel processes and run the program again but still have same error.
I wrote it because I don't believe that he uses constant on that place. I would expect that he is constructing file name and that he can have a bug somewhere.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.