|
-
Apr 30th, 2013, 08:52 AM
#1
Thread Starter
Hyperactive Member
VB to open An Existing Excel Workbook
How would I open an existing Excel workbook using VB.Net?
-
Apr 30th, 2013, 09:01 AM
#2
Re: VB to open An Existing Excel Workbook
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
-
Apr 30th, 2013, 09:02 AM
#3
Addicted Member
Re: VB to open An Existing Excel Workbook
 Originally Posted by Jo15765
How would I open an existing Excel workbook using VB.Net?
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
Last edited by Crzyrio; Apr 30th, 2013 at 09:53 AM.
-
Apr 30th, 2013, 09:15 AM
#4
Thread Starter
Hyperactive Member
Re: VB to open An Existing Excel Workbook
Thank you for the sample coding Crzyrio...however, on the line that reads
Code:
xls = xls.Workbooks.Open("")
-
Apr 30th, 2013, 09:45 AM
#5
Addicted Member
Re: VB to open An Existing Excel Workbook
 Originally Posted by Jo15765
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
Last edited by Crzyrio; Apr 30th, 2013 at 09:52 AM.
-
Apr 30th, 2013, 09:52 AM
#6
Thread Starter
Hyperactive Member
Re: VB to open An Existing Excel Workbook
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?)
-
Apr 30th, 2013, 09:58 AM
#7
Addicted Member
Re: VB to open An Existing Excel Workbook
 Originally Posted by Jo15765
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?)
I updated the code in both my post, take another look at it.
Was supposed to use the Excel.workbook object to use xls.Workbooks.Open, rather than the Excel.Application object.
-
Apr 30th, 2013, 10:00 AM
#8
Re: VB to open An Existing Excel Workbook
-
Apr 30th, 2013, 10:17 AM
#9
Thread Starter
Hyperactive Member
Re: VB to open An Existing Excel Workbook
I get a System.Runtime.InteropServices.COMException Debug error
-
Apr 30th, 2013, 10:39 AM
#10
Re: VB to open An Existing Excel Workbook
You do have Excel on your computer right?
-
Apr 30th, 2013, 10:45 AM
#11
Thread Starter
Hyperactive Member
Re: VB to open An Existing Excel Workbook
Haha...yes. I have Excel 2007 & Excel 2010 installed on this machine.
-
Apr 30th, 2013, 11:16 AM
#12
Addicted Member
Re: VB to open An Existing Excel Workbook
 Originally Posted by Jo15765
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.
-
Apr 30th, 2013, 11:37 AM
#13
Thread Starter
Hyperactive Member
Re: VB to open An Existing Excel Workbook
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....
-
Apr 30th, 2013, 11:45 AM
#14
Re: VB to open An Existing Excel Workbook
Please post the entire line. Not just
xlWorkBook = xlApp.Workbooks.Open....
-
Apr 30th, 2013, 11:58 AM
#15
Thread Starter
Hyperactive Member
Re: VB to open An Existing Excel Workbook
Full line reads
Code:
xlWorkBook = xlApp.Workbooks.Open("C:\\Excel\\Data\\Reports\\Daily.xls");
-
Apr 30th, 2013, 12:09 PM
#16
Re: VB to open An Existing Excel Workbook
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.
-
Apr 30th, 2013, 12:12 PM
#17
Thread Starter
Hyperactive Member
Re: VB to open An Existing Excel Workbook
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'
-
Apr 30th, 2013, 12:15 PM
#18
Re: VB to open An Existing Excel Workbook
 Originally Posted by Jo15765
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'
Wrapped in double quotes? The compiler should just see a string right?
-
Apr 30th, 2013, 12:15 PM
#19
Re: VB to open An Existing Excel Workbook
 Originally Posted by Jo15765
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'
What version of VB are you using?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Apr 30th, 2013, 12:41 PM
#20
Re: VB to open An Existing Excel Workbook
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.
-
Apr 30th, 2013, 12:52 PM
#21
Addicted Member
Re: VB to open An Existing Excel Workbook
 Originally Posted by TysonLPrice
Wrapped in double quotes? The compiler should just see a string right?
This might be a dumb question but just to double check, your not in C# are you?
 Originally Posted by TysonLPrice
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.
And this. I found out the hard way when I first started, had close to 30 Word processes open and had no idea :P
-
Apr 30th, 2013, 12:58 PM
#22
Re: VB to open An Existing Excel Workbook
 Originally Posted by Crzyrio
This might be a dumb question but just to double check, your not in C# are you?
And this. I found out the hard way when I first started, had close to 30 Word processes open and had no idea :P
I wrote a program to close them all until I finally got it straight
-
Apr 30th, 2013, 01:43 PM
#23
Thread Starter
Hyperactive Member
Re: VB to open An Existing Excel Workbook
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.
-
Apr 30th, 2013, 01:55 PM
#24
Re: VB to open An Existing Excel Workbook
Post what you are running just like Crzyrio did for you...
-
Apr 30th, 2013, 02:05 PM
#25
Thread Starter
Hyperactive Member
Re: VB to open An Existing Excel Workbook
I am running that exact code, I copied/pasted it into my Visual Studio to see if it would work for me
-
Apr 30th, 2013, 02:10 PM
#26
Re: VB to open An Existing Excel Workbook
-
Apr 30th, 2013, 02:34 PM
#27
Addicted Member
Re: VB to open An Existing Excel Workbook
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:\\...");
 Originally Posted by TysonLPrice
I wrote a program to close them all until I finally got it straight 
Never thought of doing that! lol
-
Apr 30th, 2013, 06:57 PM
#28
Re: VB to open An Existing Excel Workbook
 Originally Posted by Jo15765
I am running that exact code, I copied/pasted it into my Visual Studio to see if it would work for me
Well that explains it. Change:
workbook = xls.Workbooks.Open("C:\...")
to point at your file. 
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.
Last edited by TysonLPrice; May 1st, 2013 at 05:03 AM.
-
May 7th, 2013, 05:31 PM
#29
Re: VB to open An Existing Excel Workbook
THIS would get you started. Let me know if you are still facing problems...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread " Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|