[RESOLVED] Create a New Workbook with VB.Net-VBForums
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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    89,101

    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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  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
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    89,101

    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.

    2007-2014

    Why is my data not saved to my database? | MSDN Data Walkthroughs
    MSDN "How Do I?" Videos: VB | C#
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts) | WP8 Turnstile Feather Transition with Pivot Control
    Beginner Tutorials: VB | C# | SQL

  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 10:15 PM.

  8. #8
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,928

    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/
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  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
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,928

    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/
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.