Link to home
Start Free TrialLog in
Avatar of tiehaze
tiehaze

asked on

Opening excel file from windows form, read only = false

I am opening up an excel file from my windows form. When I go to save it from the windows form, it won't save it. Does anyone have any ideas how to get it to save? Here is what I have for opening it, and closing it:

Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim myStream As IO.Stream = Nothing
        Dim openFileDialog1 As New OpenFileDialog()
        openFileDialog1.InitialDirectory = LibraryPath
        openFileDialog1.Filter = "Excel Files (*.xls)|*.xls|All files (*.*)|*.*"
        openFileDialog1.FilterIndex = 1
        openFileDialog1.RestoreDirectory = True
        If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            FileName = openFileDialog1.FileName
            Try
                myStream = openFileDialog1.OpenFile()
                If (myStream IsNot Nothing) Then
                     oExcel = CreateObject("Excel.Application")
                     oBook = oExcel.Workbooks.Open(FileName, UpdateLinks:=False, ReadOnly:=False)
                     oSheet = oBook.Sheets("Database")
                     oBook.Activate()
                End If
            Catch Ex As Exception
                MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
            Finally
                ' Check this again, since we need to make sure we didn't throw an exception on open.
                If (myStream IsNot Nothing) Then
                    myStream.Close()
                End If
            End Try
        End If
    End Sub

    Private Sub CloseLibraryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseLibraryToolStripMenuItem.Click
        oBook.Save()
        oSheet = Nothing
        oBook = Nothing
        oExcel.Quit()
        oExcel = Nothing
    End Sub
Avatar of ElrondCT
ElrondCT
Flag of United States of America image

I do an oExcel.Workbooks.Close() before the .Quit and before setting the book to Nothing in my similar code. I'm not sure if that will make a difference or not.

If it doesn't help, can you tell us what exactly is happening? Do you get an error message, and if so, what does it say? Does the app just go on its merry way with no information provided?
Avatar of tiehaze
tiehaze

ASKER

With the original code that I posted in the example, I try to close excel and it asks me the following:

"A file named 'Library 8_20_2007 - 1.xls' already exists in this location. Do you want to replace it?

I click yes, and it goes on as if it saves, but it doesn't. It does the exact same thing when I add the oExcel.Workbooks.Close() before the .Quit and before setting the book to Nothing.

Any ideas why?
ASKER CERTIFIED SOLUTION
Avatar of ElrondCT
ElrondCT
Flag of United States of America 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