Click here to Skip to main content
15,916,693 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I try to open an Excel file I get the error: "'toets.xls' cannot be accessed. The file may be corrupted, located on a server that is not responding, or read-only." I checked all the posts on Google but didn't find an answer. I tested this with many .xls files on my pc but they all give the same error.

VB
'Get sheet name
Dim sheetName As String
Dim excelApp As New Microsoft.Office.Interop.Excel.ApplicationClass
Dim wb As Microsoft.Office.Interop.Excel.Workbook
Dim ws As Microsoft.Office.Interop.Excel.Worksheet
Try
   wb = excelApp.Application.Workbooks.Open(fileName) 'line that throws error
   ws = wb.Sheets(1)
   sheetName = ws.Name
Catch ex As Exception
   MsgBox("Error: " & ex.Message, MsgBoxStyle.Exclamation)
   Return
End Try
Posted
Updated 3-Aug-10 23:11pm
v2

Answers 1 & 2 have shown you the 'early binding'.
Alternatively, you can use 'late binding'.
This way you do not need the MS 12.0 Excel Object Library reference.

VB
Dim oExcel As Object
oExcel = CreateObject("Excel.Application")
Dim oBook As Object = oExcel.Workbooks.Open("C:\YourFile.xls", , False)
Dim oSheet As Object

For Each oSheet In oBook.Worksheets
sheetName(counter) = oSheet.Name
Next oSheet

' Do some more

oBook.Close()
 
Share this answer
 
I don't think ApplicationClass is the correct entry to create a new Excel Application instance.

Dim excel As Microsoft.Office.Interop.Excel.Application
Dim wb As Microsoft.Office.Interop.Excel.Workbook
Try
excel = New Microsoft.Office.Interop.Excel.Application
wb = excel.Workbooks.Open("c:\\test.xls")
excel.Visible = True
wb.Activate()
Catch ex As COMException
MessageBox.Show("Error accessing Excel: " + ex.ToString())
Catch ex As Exception
MessageBox.Show("Error: " + ex.ToString())
End Try



VB
Dim CellRead As String
Dim excel As Microsoft.Office.Interop.Excel.Application
Dim wb As Microsoft.Office.Interop.Excel.Workbook
Dim ws As Microsoft.Office.Interop.Excel.Worksheet
Dim wr As Microsoft.Office.Interop.Excel.Range

Or using CreateObject and cast it to an Excel.Application:

<pre>
Try
excel = CType(CreateObject("Excel.Application"), Microsoft.Office.Interop.Excel.Application)

wb = excel.Workbooks.Open("C:\Yourfile.xls")
excel.Visible = True
wb.Activate()
Catch ex As Exception
MessageBox.Show("Error: " + ex.ToString())
End Try
ws = wb.Sheets(1)
wr = ws.Range("A1", "AB25")
CellRead = wr.Cells.FormulaR1C1(1, 1)



Good luck!
 
Share this answer
 
v2
Comments
Hardus Lombaard 4-Aug-10 7:31am    
Unfortunately I get the exact same error.
E.F. Nijboer 4-Aug-10 12:00pm    
You tried both examples? Did you try it with a local file or only a network file? Can you open the file with the StreamReader to rule out missing authorisation and what not?
Hi,

First Use,

Microsoft.Office.Interop.Excel.Application instead of Microsoft.Office.Interop.Excel.ApplicationClass.


Also chek your reference folder of your application, which office library reference arr you using currently, remove any old office library reference and add

Microsoft Office 12.0 Object Library
(inside Com).

Please reply if helpful,


Thanks
Vaibhav J
 
Share this answer
 
Comments
Hardus Lombaard 4-Aug-10 7:51am    
I added "Microsoft Office 12.0 Object Library" and "Microsoft Excel 12.0 Object Library" and I use Microsoft.Office.Interop.Excel.Application instead of Microsoft.Office.Interop.Excel.ApplicationClass but I get the same error. This is interesting however: It works fine with .xlsx files, but not with the older .xls files. Any idea how to make it work with .xls also?
Have you tried saving the file as an xlsx file?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900