Click here to Skip to main content
15,885,366 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Plz Help Me For Get HDD Serial No Pin
Henry Minute4-Jul-09 0:39
Henry Minute4-Jul-09 0:39 
GeneralRe: Plz Help Me For Get HDD Serial No Pin
Christian Graus4-Jul-09 15:10
protectorChristian Graus4-Jul-09 15:10 
Question3D project for VB.Net Pin
phowarso3-Jul-09 20:29
phowarso3-Jul-09 20:29 
AnswerRe: 3D project for VB.Net Pin
Henry Minute4-Jul-09 0:13
Henry Minute4-Jul-09 0:13 
AnswerRe: 3D project for VB.Net Pin
LloydA11111-Jul-09 1:43
LloydA11111-Jul-09 1:43 
Questionenumerate Microsoft sql server instance in local network Pin
Rupesh Kumar Swami3-Jul-09 5:53
Rupesh Kumar Swami3-Jul-09 5:53 
AnswerRe: enumerate Microsoft sql server instance in local network Pin
Henry Minute3-Jul-09 8:53
Henry Minute3-Jul-09 8:53 
QuestionProblem Reading, Saving from Excel file Pin
Trupti Mehta3-Jul-09 5:48
Trupti Mehta3-Jul-09 5:48 
Hello,

I have to read an Excel file, update some data and save the file. I wrote small code to open, read, save and close an excel file. I am using Microsoft.Office.Interop to achieve the goal. I have set the Excel application visibility as false. I tried the same in C# and VB.NET, but am facing same problem. Code is below:-

Private Sub OpenFile()
    Try
        excelApp = New Excel.ApplicationClass()
        excelApp.Visible = False
        excelworkbook = excelApp.Workbooks.Open(workbookPath, 0, False, 5, "", "", False, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", True, False, 0, False, False, False)
        excelSheets = excelworkbook.Worksheets
        excelWorkSheet = excelworkbook.Worksheets("Sheet1")
        MessageBox.Show("Connected and Opened")
        range = excelWorkSheet.UsedRange
        MsgBox("Rows = " + range.Rows.Count.ToString + " Cols = " + range.Columns.Count.ToString)
    Catch e As Exception
        excelSheets = Nothing
        excelworkbook = Nothing
        excelWorkSheet = Nothing
        excelApp = Nothing
        MessageBox.Show("Exception MSG " + e.Message + "\n ST :" + e.StackTrace)
    End Try
End Sub

Private Function ReadRow(ByVal rowNo As Integer, ByVal startRange As String, ByVal endRange As String) As Array
    Dim rge As Excel.Range = Nothing
    Dim valuesList As Array = Nothing

    Try
        rge = excelWorkSheet.Range(startRange, endRange)
        valuesList = rge.Cells.Value
    Catch e As Exception
        MsgBox("Uable To Read from " + startRange + " To " + endRange + " Due to " + vbCrLf + e.StackTrace)
        Save_Close()
    End Try

    Return valuesList
End Function

Private Sub Save_Close()
    Try
        excelworkbook.Save()
        excelworkbook.Close()
    Catch ee As Exception
        MessageBox.Show("Unable To Save : " + ee.Message)
        excelworkbook.Close()
    End Try

    releaseObject(excelApp)
    releaseObject(excelworkbook)
    releaseObject(excelWorkSheet)
    releaseObject(excelSheets)

    MessageBox.Show("File Closed")
End Sub

Private Sub releaseObject(ByVal obj As Object)
    Try
        System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub



Methods I have called is :
OpenFile()
ReadRow(1, A1, AL)
Save_Close


The problem I am facing is:
For REad Row: I get Exception
Unable to Read from A1 to AL Due to
Exception from HRESULT : 0X800A03EC
st: AT ....WorkSheet.getRange(Object Cell1, Object Cell2) at ....Line no

I tried the same code with ReadRow parameters as 1, A1, I6) and I got an array successfully. I want to read contents of a row of the provided range.

On calling excelworkbook.Save() in Save_Close(), I get exception as follows and the system halts/hangs on close line. I have to Stop Debugging.
Unable to save: Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))


After once executing, the next time I run the application, I get on start, "File is being used by , want to open as read-only?" with Yes/No options.

Can anybody help me know where am I going wrong. The file is .xls file with fixed 1st 3 cols and has several cols till CL or even more. My OS is Vista. I have added reference of Microsoft.Office.Interop.Excel version 12. I tried with 11 also, but faced same error. I want to edit the file also, so it should be opened with ability to update and not just Read-Only.


Any help is appreciated. Please try to help at the earliest. I am in big trouble.

Thanks

Terry

QuestionDateTimePicker problem with Month and Day Pin
Nielvh3-Jul-09 3:12
Nielvh3-Jul-09 3:12 
AnswerRe: DateTimePicker problem with Month and Day Pin
Nagy Vilmos3-Jul-09 3:46
professionalNagy Vilmos3-Jul-09 3:46 
GeneralRe: DateTimePicker problem with Month and Day Pin
Nielvh3-Jul-09 3:59
Nielvh3-Jul-09 3:59 
GeneralRe: DateTimePicker problem with Month and Day Pin
Enver Maroshi3-Jul-09 4:06
Enver Maroshi3-Jul-09 4:06 
GeneralRe: DateTimePicker problem with Month and Day Pin
Nielvh3-Jul-09 4:15
Nielvh3-Jul-09 4:15 
GeneralRe: DateTimePicker problem with Month and Day Pin
Enver Maroshi3-Jul-09 4:23
Enver Maroshi3-Jul-09 4:23 
GeneralRe: DateTimePicker problem with Month and Day Pin
Nielvh3-Jul-09 4:33
Nielvh3-Jul-09 4:33 
GeneralRe: DateTimePicker problem with Month and Day Pin
Nielvh5-Jul-09 23:34
Nielvh5-Jul-09 23:34 
QuestionWeb Query- Com Addin Excel 2003 Pin
YakomozB3-Jul-09 2:57
YakomozB3-Jul-09 2:57 
QuestionHow to show DataTable in ReportViewer contral as Local Report (.rdlc) Pin
angel0in2-Jul-09 22:30
angel0in2-Jul-09 22:30 
AnswerRe: How to show DataTable in ReportViewer contral as Local Report (.rdlc) Pin
anas.safar14-Nov-10 22:11
anas.safar14-Nov-10 22:11 
QuestionHow to unlock a PC programmatically [modified] Pin
Uwe1234562-Jul-09 21:46
Uwe1234562-Jul-09 21:46 
AnswerRe: How to unlock a PC programmatically Pin
Enver Maroshi3-Jul-09 2:44
Enver Maroshi3-Jul-09 2:44 
GeneralRe: How to unlock a PC programmatically Pin
Uwe1234563-Jul-09 3:01
Uwe1234563-Jul-09 3:01 
GeneralRe: How to unlock a PC programmatically Pin
Enver Maroshi3-Jul-09 3:28
Enver Maroshi3-Jul-09 3:28 
GeneralRe: How to unlock a PC programmatically Pin
Uwe1234563-Jul-09 3:33
Uwe1234563-Jul-09 3:33 
GeneralRe: How to unlock a PC programmatically Pin
Enver Maroshi3-Jul-09 3:40
Enver Maroshi3-Jul-09 3:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.