Results 1 to 29 of 29

Thread: VB to open An Existing Excel Workbook

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    VB to open An Existing Excel Workbook

    How would I open an existing Excel workbook using VB.Net?

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    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

  3. #3
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: VB to open An Existing Excel Workbook

    Quote Originally Posted by Jo15765 View Post
    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:
    1. Imports Microsoft.Office.Interop
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    6.  
    7.         Dim xls As Excel.Application
    8.         Dim workbook As Excel.Workbook
    9.         xls = New Excel.Application
    10.  
    11.         xls.Visible = True
    12.  
    13.         workbook = xls.Workbooks.Open("C:\...")
    14.  
    15.     End Sub
    16. 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.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    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("")

  5. #5
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: VB to open An Existing Excel Workbook

    Quote Originally Posted by Jo15765 View Post
    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:
    1. Imports Microsoft.Office.Interop
    2.  
    3. Public Class Form1
    4.  
    5.     Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    6.  
    7.         Dim xls As Excel.Application
    8.         Dim workbook As Excel.Workbook
    9.         xls = New Excel.Application
    10.  
    11.         xls.Visible = True
    12.  
    13.         workbook = xls.Workbooks.Open("C:\...")
    14.  
    15.     End Sub
    16. End Class
    Last edited by Crzyrio; Apr 30th, 2013 at 09:52 AM.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    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?)

  7. #7
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: VB to open An Existing Excel Workbook

    Quote Originally Posted by Jo15765 View Post
    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.

  8. #8
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: VB to open An Existing Excel Workbook

    Worked for me...

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: VB to open An Existing Excel Workbook

    I get a System.Runtime.InteropServices.COMException Debug error

  10. #10
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: VB to open An Existing Excel Workbook

    You do have Excel on your computer right?

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: VB to open An Existing Excel Workbook

    Haha...yes. I have Excel 2007 & Excel 2010 installed on this machine.

  12. #12
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: VB to open An Existing Excel Workbook

    Quote Originally Posted by Jo15765 View Post
    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.

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    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....

  14. #14
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: VB to open An Existing Excel Workbook

    Please post the entire line. Not just

    xlWorkBook = xlApp.Workbooks.Open....

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    Re: VB to open An Existing Excel Workbook

    Full line reads
    Code:
    xlWorkBook = xlApp.Workbooks.Open("C:\\Excel\\Data\\Reports\\Daily.xls");

  16. #16
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    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.

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    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'

  18. #18
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: VB to open An Existing Excel Workbook

    Quote Originally Posted by Jo15765 View Post
    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?

  19. #19
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: VB to open An Existing Excel Workbook

    Quote Originally Posted by Jo15765 View Post
    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!

  20. #20
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    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.

  21. #21
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: VB to open An Existing Excel Workbook

    Quote Originally Posted by TysonLPrice View Post
    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?


    Quote Originally Posted by TysonLPrice View Post
    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

  22. #22
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: VB to open An Existing Excel Workbook

    Quote Originally Posted by Crzyrio View Post
    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

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    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.

  24. #24
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: VB to open An Existing Excel Workbook

    Post what you are running just like Crzyrio did for you...

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2010
    Posts
    272

    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

  26. #26
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: VB to open An Existing Excel Workbook

    OK...good luck to you.

  27. #27
    Addicted Member
    Join Date
    Jan 2013
    Posts
    177

    Re: VB to open An Existing Excel Workbook

    Try this :P

    Paste it into a form load or a button

    vb.net Code:
    1. Microsoft.Office.Interop.Excel.Application oXL;
    2.             Microsoft.Office.Interop.Excel._Workbook oWB;
    3.  
    4.  
    5.             oXL = new Microsoft.Office.Interop.Excel.Application();
    6.  
    7.             oXL.Visible = true;
    8.  
    9.             oWB = oXL.Workbooks.Open("C:\\...");



    Quote Originally Posted by TysonLPrice View Post
    I wrote a program to close them all until I finally got it straight
    Never thought of doing that! lol

  28. #28
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: VB to open An Existing Excel Workbook

    Quote Originally Posted by Jo15765 View Post
    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.

  29. #29
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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
  •  



Click Here to Expand Forum to Full Width