Results 1 to 11 of 11

Thread: [RESOLVED] Create a New Workbook with VB.Net

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    18

    Resolved [RESOLVED] Create a New Workbook with VB.Net

    I thought this would work, but it doesn't:
    Code:
    Dim newWorkbook As New Excel.Workbook = Me.Application.Workbooks.Add()
    And this gives an error of end of statement expected

    How can I add a workbook using VB.Net?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Create a New Workbook with VB.Net

    This part:
    Code:
    Dim newWorkbook As New Excel.Workbook
    is equivalent to this:
    Code:
    Dim newWorkbook As Excel.Workbook = New Excel.Workbook
    so it doesn't make sense to do this:
    Code:
    Dim newWorkbook As Excel.Workbook = New Excel.Workbook = Me.Application.Workbooks.Add()
    does it? You can only assign one value to a variable on a line. Either you use the New keyword to invoke a constructor to create the object to assign or you call the Add method to create the object to assign, not both. In this case and in all others, the New keyword is NOT part of the declaration. It creates a new object so only use it if you want to create a new object.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    18

    Re: Create a New Workbook with VB.Net

    I modified my code to this:
    Code:
    Application.Workbooks.Worksheet.Add()
    Would that be a sufficient way of adding a workbook with VB.Net? If so, I am getting an error of NullReferenceException was unhandled

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    18

    Re: Create a New Workbook with VB.Net

    Quote Originally Posted by RichardSmith View Post
    I modified my code to this:
    Code:
    Application.Workbooks.Worksheet.Add()
    Would that be a sufficient way of adding a workbook with VB.Net? If so, I am getting an error of NullReferenceException was unhandled
    I found this code and it works, but I don't understand why it is different than what I posted above.
    Code:
            Dim excelApp As New Excel.Application
            Dim excelBook As Excel.Workbook = excelApp.Workbooks.Add()

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: Create a New Workbook with VB.Net

    Quote Originally Posted by RichardSmith View Post
    I modified my code to this:
    Code:
    Application.Workbooks.Worksheet.Add()
    Would that be a sufficient way of adding a workbook with VB.Net? If so, I am getting an error of NullReferenceException was unhandled
    That's going to add a new workbook but it doesn't assign it to a variable so you have no access to it afterwards. If you don't assign anything to your variable then you can't expect your variable to have a value.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    18

    Re: Create a New Workbook with VB.Net

    Ah that makes sense than as to why:
    Code:
            Dim excelApp As New Excel.Application
            Dim excelBook As Excel.Workbook = excelApp.Workbooks.Add()
    This worked because I assigned the variable excelBook to it! Thank you!

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    18

    Re: Create a New Workbook with VB.Net

    I did read your 1st post, it just didn't click until I saw it in action. I am very new to VB.Net and am trying to learn the ins and outs on my own/forum assistance.

    Do I need to actually add a worksheet to the workbook that I just created or does it automatically add worksheets to the workbook when it's created?
    Last edited by RichardSmith; Oct 1st, 2012 at 09:15 PM.

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

    Re: Create a New Workbook with VB.Net

    Richard, if you are interested in Excel automation then I would recommend this link

    Topic: VB.NET and Excel
    Link: http://www.siddharthrout.com/vb-dot-net-and-excel/
    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

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    18

    Re: Create a New Workbook with VB.Net

    Quote Originally Posted by koolsid View Post
    Richard, if you are interested in Excel automation then I would recommend this link

    Topic: VB.NET and Excel
    Link: http://www.siddharthrout.com/vb-dot-net-and-excel/
    That site is a tremendous help, thank you for the assistance. One question that I do not see covered....How can I take a selection and make the font bold?
    Code:
    Dim xlsApp As Excel.Application
    Dim xlsWB As Excel.Workbook
    Dim xlsSheet As Excel.Worksheet
    xlsSheet.Range("A1:W1").Select()
    xlsApp.xlsWB.Selection.Font.Bold = True

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

    Re: Create a New Workbook with VB.Net

    Quote Originally Posted by RichardSmith View Post
    One question that I do not see covered....How can I take a selection and make the font bold?
    Are you sure ?

    Topic: Formatting Text
    Link: http://www.siddharthrout.com/2012/09...rmatting-text/
    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

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Oct 2012
    Posts
    18

    Re: Create a New Workbook with VB.Net

    I stand corrected! Thank you again! Amazing guide btw

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