How would I open an existing Excel workbook using VB.Net?
Printable View
How would I open an existing Excel workbook using VB.Net?
You should check out the search facilty here...you wouldn't have to wait. I just grabbed this from what came back. There is more:
http://www.vbforums.com/showthread.p...Excel+workbook
Do you want to open it in a vb form or in Excel?
If you want to do it in excel,
vb.net Code:
Imports Microsoft.Office.Interop Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Dim xls As Excel.Application Dim workbook As Excel.Workbook xls = New Excel.Application xls.Visible = True workbook = xls.Workbooks.Open("C:\...") End Sub End Class
http://support.microsoft.com/kb/301982
If you want to open it in the form it self, your best bet is to use the Webbrowser control to open the file
Thank you for the sample coding Crzyrio...however, on the line that reads
Code:xls = xls.Workbooks.Open("")
What happens on that line? The path of your excel document goes there.
EDIT: My Mistake, this will work for sure. You need a workbook object when opening a workbook :P
vb.net Code:
Imports Microsoft.Office.Interop Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Dim xls As Excel.Application Dim workbook As Excel.Workbook xls = New Excel.Application xls.Visible = True workbook = xls.Workbooks.Open("C:\...") End Sub End Class
I get a compile error of Cannot implicitly convert type Microsoft.Office.Interop.Excel.Workbook to Microsoft.Office.Interop.Excel.Application. An explicit conversion exists (are you missing a cast?)
Worked for me...
I get a System.Runtime.InteropServices.COMException Debug error
You do have Excel on your computer right? :D
Haha...yes. I have Excel 2007 & Excel 2010 installed on this machine.
Did you copy paste the exact code? Check if there are any spaces in your filepath name, that can cause problems sometimes.
Which reference did you add in order to use this? There is a 12.0 Object Library and a 5.0 Library, I am using the 12.0 Library with Excel 2007.
Does the code compile? At what point do you get the error.
Yes, I copied and pasted the code exactly as it appears.
I added a reference to the Microsoft Excel 12.0 Object Library -- and this is a COM reference
The code compiles fine, it produces the error when it reaches this line
Code:xlWorkBook = xlApp.Workbooks.Open....
Please post the entire line. Not just
xlWorkBook = xlApp.Workbooks.Open....
Full line reads
Code:xlWorkBook = xlApp.Workbooks.Open("C:\\Excel\\Data\\Reports\\Daily.xls");
That's new to me but that doesn't mean a whole lot. I would have used:
xlWorkBook = xlApp.Workbooks.Open("C:\Excel\Data\Reports\Daily.xls");
The version of Excel I'l using creates .xlxs extentions also. Still works with the older stuff though.
That's interesting a single slash compiles on your end. When I try a single slash I get a compile error of 'Unrecognized Escape Sequence'
OK...I'm going to try and save you some headaches down the road based on I think you are new to manipulating Excel based on your post. Closing and disposing of Excel is just as important as opening it. After running the application and few times and getting odd errors you'll notice in task manager multiple versions of Excel running. That means it wasn't cleaned up properly. Search this site for "Excel keeps running in task manager" for some solutions.
I am working with a VB project...but thanks for checking :) ....
I just rebooted to see if there were any lingering instances of Excel going that could be causing my peculiar error, but alas, even after a clean start my coding is still producing that same error.
Post what you are running just like Crzyrio did for you...
I am running that exact code, I copied/pasted it into my Visual Studio to see if it would work for me
OK...good luck to you.
Try this :P
Paste it into a form load or a button
vb.net Code:
Microsoft.Office.Interop.Excel.Application oXL; Microsoft.Office.Interop.Excel._Workbook oWB; oXL = new Microsoft.Office.Interop.Excel.Application(); oXL.Visible = true; oWB = oXL.Workbooks.Open("C:\\...");
Never thought of doing that! lol
Well that explains it. Change:
workbook = xls.Workbooks.Open("C:\...")
to point at your file. :cool:
You might be better served by reviewing this tutorial:
http://vb.net-informations.com/
Go to the section on VB.NET Excel 2007 Tutorials.
Last time I was there all the examples worked as posted. I'm on VS 2012 some some tweaks were needed but it will give you a bigger picture.
Also look at:
http://www.vbforums.com/showthread.p...6-(or-VB5-VBA)
It is VB 6.0 but the principles are the same.
THIS would get you started. Let me know if you are still facing problems...