-
Oct 1st, 2012, 08:13 PM
#1
Thread Starter
Junior Member
[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?
-
Oct 1st, 2012, 08:19 PM
#2
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.
-
Oct 1st, 2012, 08:27 PM
#3
Thread Starter
Junior Member
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
-
Oct 1st, 2012, 08:42 PM
#4
Thread Starter
Junior Member
Re: Create a New Workbook with VB.Net
Originally Posted by RichardSmith
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()
-
Oct 1st, 2012, 08:58 PM
#5
Re: Create a New Workbook with VB.Net
Originally Posted by RichardSmith
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.
-
Oct 1st, 2012, 09:04 PM
#6
Thread Starter
Junior Member
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!
-
Oct 1st, 2012, 09:09 PM
#7
Thread Starter
Junior Member
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.
-
Oct 1st, 2012, 11:44 PM
#8
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
-
Oct 2nd, 2012, 08:11 AM
#9
Thread Starter
Junior Member
Re: Create a New Workbook with VB.Net
Originally Posted by koolsid
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
-
Oct 2nd, 2012, 08:19 AM
#10
Re: Create a New Workbook with VB.Net
Originally Posted by RichardSmith
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
-
Oct 2nd, 2012, 08:25 AM
#11
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|