Hi, I have recently installed Microsoft Visual Basic 2005 Express Edition and would like to open an existing Excel file from a command button within the application I am making.
Having scanned through the help and forum sites i have made the following code which does not provide any errors, however when I click the command button the mouse cursor changes to the 'Busy' cursor then nothing happens.
Here is my code:
Private
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim xlTmp As Microsoft.Office.Interop.Excel.ApplicationxlTmp =
New Microsoft.Office.Interop.Excel.ApplicationxlTmp.Workbooks.Open(
"c:\documents and settings\andy\desktop\test.xls") End SubI have added the Microsft Excell Object library reference to my project. Any help in correcting the code would be much appreciated.
Thanks in anticipation,
Andy

Open an existing Excel File
TimLB
Andy,
In order to see the instance of Excel that you created, you must make it visible. Make the last line of code:
TrueAlso, you should capture and maintain a reference to the Workbook after opening. If you do not explicitly close the Workbook at some point, that instance of Excel will still be running after your application exits.
Ray Dyce
Regards, Andy
dandoyon
That's ok, we were all new at one point or another
You should create an instance of an Excel Workbook at the class level (that is, outside of your Button_Click event) and then set that instance equal to the Workbook object you opened. Then, when your application ends, you can also ensure that Excel closes.
Now, since you are displaying the Excel application it's really not that big of a deal - the user can just close Excel themselves. But just for your own reference, if you ever decide to automate Excel behind the scenes, it is good to know that Excel won't close itself just because the app that started it has ended (you could quickly find many many instances of Excel running in the Task Manager that have no visible UI).
Here is a simple expansion of your code to give an example:
Dim wb As Microsoft.Office.Interop.Excel.Workbook Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim xlTmp As Microsoft.Office.Interop.Excel.ApplicationxlTmp =
New Microsoft.Office.Interop.Excel.Applicationwb = xlTmp.Workbooks.Open("test.xls")
xlTmp.Visible =
True End Sub Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing End SubHope that helps!
Tom Morris