Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with saving file using Ecxel

Status
Not open for further replies.

mil2n

Programmer
Feb 28, 2005
4
I don’t know how to save a file using Excel application. Once I open the file it is read only and I am not able to save it.

Can someone help me with this problem?


Dim excelApp As New Excel.Application()
Dim excelBook As Excel.Workbook
excelBook = excelApp.Workbooks.Open(Path)
Dim excelWorksheet As Excel.Worksheet = CType(excelBook.Worksheets(1), Excel.Worksheet)

With excelWorksheet
.Range("A1").Value = "somethnig"
End With

Try
excelBook.SaveAs(Path) ' Here is the problem, opened file is read only and could not by saved
Catch ed As Exception
End Try

Try

excelWorksheet = Nothing
excelBook = Nothing
excelApp.Quit()
excelApp = Nothing
GC.Collect()

 
and this works?

With excelWorksheet
.Range("A1").Value = "somethnig"
End With

are you sure the file isn't open in excell? or another program.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
If you manually try to open your Excel book, you'll probably get "<your xls> is locked for editing".

This is because "When Visual Studio .NET calls a COM object from managed code, it automatically creates a Runtime Callable Wrapper (RCW). The RCW marshals calls between the .NET application and the COM object. The RCW keeps a reference count on the COM object. Therefore, if all references have not been released on the RCW, the COM object does not quit."

When ending an Excel session from your application, make sure all references are released.

Follow the instructions on this page to correct your code:



__________________________________________
Try forum1391 for lively discussions
 
It is working. Thanks a lot.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top