troubleshooting Question

Opening excel file from windows form, read only = false

Avatar of tiehaze
tiehaze asked on
.NET ProgrammingVisual Basic.NETMicrosoft Development
3 Comments1 Solution205 ViewsLast Modified:
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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros