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_Clic k(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Clic k
Dim myStream As IO.Stream = Nothing
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDir ectory = LibraryPath
openFileDialog1.Filter = "Excel Files (*.xls)|*.xls|All files (*.*)|*.*"
openFileDialog1.FilterInde x = 1
openFileDialog1.RestoreDir ectory = True
If openFileDialog1.ShowDialog () = System.Windows.Forms.Dialo gResult.OK Then
FileName = openFileDialog1.FileName
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
oExcel = CreateObject("Excel.Applic ation")
oBook = oExcel.Workbooks.Open(File Name, 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 CloseLibraryToolStripMenuI tem_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseLibraryToolStripMenuI tem.Click
oBook.Save()
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
End Sub
Private Sub OpenToolStripMenuItem_Clic
Dim myStream As IO.Stream = Nothing
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDir
openFileDialog1.Filter = "Excel Files (*.xls)|*.xls|All files (*.*)|*.*"
openFileDialog1.FilterInde
openFileDialog1.RestoreDir
If openFileDialog1.ShowDialog
FileName = openFileDialog1.FileName
Try
myStream = openFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
oExcel = CreateObject("Excel.Applic
oBook = oExcel.Workbooks.Open(File
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 CloseLibraryToolStripMenuI
oBook.Save()
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
End Sub
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?
"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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?