I thought this would work, but it doesn't:
And this gives an error of end of statement expectedCode:Dim newWorkbook As New Excel.Workbook = Me.Application.Workbooks.Add()
How can I add a workbook using VB.Net?
Printable View
I thought this would work, but it doesn't:
And this gives an error of end of statement expectedCode:Dim newWorkbook As New Excel.Workbook = Me.Application.Workbooks.Add()
How can I add a workbook using VB.Net?
This part:is equivalent to this:Code:Dim newWorkbook As New Excel.Workbook
so it doesn't make sense to do this:Code:Dim newWorkbook As Excel.Workbook = New Excel.Workbook
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.Code:Dim newWorkbook As Excel.Workbook = New Excel.Workbook = Me.Application.Workbooks.Add()
I modified my code to this:
Would that be a sufficient way of adding a workbook with VB.Net? If so, I am getting an error of NullReferenceException was unhandledCode:Application.Workbooks.Worksheet.Add()
Ah that makes sense than as to why:
This worked because I assigned the variable excelBook to it! Thank you! :)Code:Dim excelApp As New Excel.Application
Dim excelBook As Excel.Workbook = excelApp.Workbooks.Add()
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?
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
Are you sure :) ?
Topic: Formatting Text
Link: http://www.siddharthrout.com/2012/09...rmatting-text/
I stand corrected! Thank you again! Amazing guide btw