Link to home
Start Free TrialLog in
Avatar of sridhar79
sridhar79

asked on

COMException while opening excel file

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
Avatar of CJ_S
CJ_S
Flag of Netherlands image

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
Avatar of sridhar79
sridhar79

ASKER

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
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
ASKER CERTIFIED SOLUTION
Avatar of CJ_S
CJ_S
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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..