reading and writing to excel from

Posted on 2006-06-08
Last Modified: 2011-10-03
hello experts, I want to read and write to an excel file and it seems to work except the workbook doesnt close and my changes arent always being saved can someone look at my code and see what I am doing wrong?  

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim oExcel As Excel.Application
        Dim oWorkBook As Excel.Workbook
        Dim oWorkSheet As Excel.Worksheet
        Dim j As Integer
        j = 1
        oExcel = New Excel.Application
        oWorkBook = oExcel.Workbooks.Open("C:\smallcount\smallframe.xls")
        oWorkSheet = oWorkBook.Worksheets().Item(1)
        Dim o As Excel.Range = oWorkSheet.Cells.Item(j, 1)
        Dim s As String = o.Cells.Text
        Do While s <> ""
            j += 1
            o = o.Cells.Item(j, 1)
            s = o.Cells.Text
        o.Cells(j, 1).Value = "Testvalue"
        oWorkBook.Application.DisplayAlerts = False
Question by:tentavarious
    LVL 4

    Accepted Solution

    Try killing the application object after you close Excel.

    If Not oExcel Is Nothing Then
        oExcel = Nothing
    End If


    This will kill all processes of Excel.

                Dim currentProcess() As Process = Process.GetProcessesByName("excel")
                Dim proc As Process

                For Each proc In currentProcess
            Catch ex As Exception
                MsgBox("Error closing Excel2: " & ex.Message)
            End Try

    I don't know why the save isn't working.  I use the document object and save this way:

    I hope this helps.

    Author Comment

    Still not working when I try to open the sheet I get a message saying its already opened and reopening will cause any saved data to be lost.
    LVL 4

    Expert Comment

    Did you try the process method I entered?  That should definitely kill all instances of Excel.

    Author Comment

    The problem is the save method i am getting this error       Message      "Cannot access read-only document 'smallframe.xls'."      String

    Expert Comment

    I get an error with Excel.Application is nto defined!

    when using
     Dim oExcel As Excel.Application

    in Visual Studio VB 2008

    Author Comment

    I havent used excel objects in 2008 yet, but almost sounds like your missing a reference.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now