COMException while opening excel file

sridhar79
sridhar79 used Ask the Experts™
on
Hi,
   I am trying to open an excel file using .NET's excel object. It was working fine before.. I dont know why it started giving me a COMException error recently.. I am trying to figure why but I cant.. My excel is not protected or locked.. But still it gives me the same error. This is what I coded:

Dim strcon As String
        strcon = "PROVIDER = Microsoft.jet.OLEDB.4.0; Data Source = " & _
                Me.txtTemplateFile.Text & "; Extended Properties = Excel 8.0 "
        Dim olecon As OleDb.OleDbConnection
        olecon = New OleDb.OleDbConnection(strcon)
        olecon.Open()

        Dim table As DataTable
        Dim objxl As Excel.Application
        objxl = New Excel.Application()
        Dim wbxs As Excel.Workbooks
        Dim wbxl As Excel.Workbook

        Dim xl_file As String
        xl_file = Me.txtTemplateFile.Text

        wbxs = objxl.Workbooks
        wbxl = wbxs.Add

        wbxl = wbxs.Open(xl_file)


and this is the error it generates

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in WindowsApplication2.exe

Additional information: Cannot access 'Lab_Data_Sheet.xls'.

Any comments or suggestions would be greatly appreciated.

Thanks,
Yash
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
The message says it cannot access the excel sheet. First check whether the path is correct or not. For testing purposes you can put the fully qwualified path in the open method. Next thing to look at is the security context your application is working in. Do you yourself have sufficient rights to access the file?

Last thing to check is the event viewer, it may have some more details for you!

CJ

Author

Commented:
CJ,
  I tried putting fully qualified path but it still gives me the same error. I tried to catch the exception it gave me the following:

Unhandled Exception: System.Runtime.InteropServices.COMException (0x800A03EC): Cannot access 'Lab_Data_Sheet.xls'.
   at Excel.Workbooks.Open(String Filename, Object UpdateLinks, Object ReadOnly, Object Format, Object Password, Object WriteResPassword, Object IgnoreReadOnlyRecommended, Object Origin, Object Delimiter, Object Editable, Object Notify, Object Converter, Object AddToMru)

Can you figure out the glitch from this detailed error catch?

Thanks,
Yash

Author

Commented:
It was working fine last week as I was opening another xls file and reading the data from it. Now even that file is not being read. I could not go into the event viewer as I dont have enough admin rights. I checked the security settings and not even a single xls file has protection on it.. It is kind of confusing for me.. Any suggestions??
Yash
Commented:
Can't see it. What happens if you create a new test application to access that file. Can you still access other .xls files with your code?

Author

Commented:
Hi,
  I figured out the glitch. As you can see from the code I was trying to open the file which already got open using oledb. It wasn't opening as it was already open. Thanks for your suggestions anyway..

 

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial