Dec 9th, 2006, 02:33 PM
#1
[FAQ's: OD] How do I create a new workbook?
If you need to programmatically create a new blank workbook from within Excel itself you would use the Application.Workbooks.Add method. Now if you are automating Excel from another program language like VB 6 or a .NET language then you would create the application instance and add a new blank workbook.
From within the Excel VBA IDE environment you can place your code to create a new workbook within your Excel Application instance like shown below.
Excel 2003 VBA Code Example:
VB Code:
Option Explicit
'Behind "ThisWorkbook" class
Public Sub AddNewWorkbook()
'Define a workbook variable in case you want to perform any modifications on the workbook when it is created
Dim oWB As Excel.Workbook
'Add it to the Workbooks collection and set our object variable equal to the newly added workbook
'By referencing the "Application" object instance we are adding the workbook to the current
'runnng Excel instance (since we are in VBA).
Set oWB = Application.Workbooks.Add
'Perform and mods to the new workbook.
'...
'Close the new workbook, Save it and clean up resources
oWB.Close SaveChanges:=True, FileName:="C:\NewWorkbook.xls"
Set oWB = Nothing
End Sub
Then to call the procedure from any sheet or module ....
VB Code:
Option Explicit
'Behind Sheet1
Sub Button1_Click()
AddNewWorkbook
End Sub
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Dec 9th, 2006, 02:34 PM
#2
Re: [FAQ's: OD] How do I create a new workbook?
VB 6 and Excel 2003 Early Binding Code Example:
VB Code:
Option Explicit
'Add a reference to MS Excel xx.0 Object Library
Private Sub Command1_Click()
Dim oApp As Excel.Application
Dim oWB As Excel.Workbook
'Create an Excel instance.
Set oApp = New Excel.Application
'Create a new workbook
Set oWB = oApp.Workbooks.Add
'Perform and mods to the new workbook.
'...
'Close the new workbook, Save it and clean up resources
oWB.Close SaveChanges:=True, FileName:="C:\NewWorkbook.xls"
Set oWB = Nothing
'Quit/Close the Excel application
oApp.Quit
Set oApp = Nothing
End Sub
VB 6 and Excel 2003 Late Binding Code Example:
VB Code:
Option Explicit
Private Sub Command1_Click()
Dim oApp As Object
Dim oWB As Object
'Create an Excel instance.
Set oApp = CreateObject("Excel.Application")
'Create a new workbook
Set oWB = oApp.Workbooks.Add
'Perform and mods to the new workbook.
'...
'Close the new workbook, Save it and clean up resources
oWB.Close SaveChanges:=True, FileName:="C:\NewWorkbook.xls"
Set oWB = Nothing
'Quit/Close the Excel application
oApp.Quit
Set oApp = Nothing
End Sub
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
Dec 9th, 2006, 04:12 PM
#3
Re: [FAQ's: OD] How do I create a new workbook?
VB.NET 2003 And Excel 2003 Early Binding Code Example:
VB Code:
Option Explicit On
Option Strict On
'Copyright © 2006 by RobDog888 (VB/Office Guru™). All Rights reserved.
'
' You may not reproduce or publish this
' code on any web site, online service,
' or distribute as source on any media
' without express permission.
'Add a reference to Microsoft Excel xx.0 Object Library
Imports Microsoft.Office.Interop
Public Class Form1
Inherits System.Windows.Forms.Form
"Windows Form Designer generated code"
Private oApp As Excel.Application
Private oWB As Excel.Workbook
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
Me.Close()
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.Closing
'CHECK IF THE WORKBOOK WAS SAVED FROM THE USER IN EXCEL
If TypeName(oWB) <> "Nothing" Then
If oWB.Saved = False Then
Dim dlgSaveFile As New SaveFileDialog
With dlgSaveFile
.CheckFileExists = True
.CheckPathExists = True
.Filter = "Excel Files Only (*.xls)|*.xls"
.FilterIndex = 0
.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
.CreatePrompt = True
.FileName = "Book1.xls"
.OverwritePrompt = True
.ValidateNames = True
.Title = "Select an Excel Workbook File"
If .ShowDialog = DialogResult.OK Then
oWB.SaveAs(FileName:=.FileName)
oWB.Saved = True
Else
'CANCECL WAS PRESSED SO PROMPT TO CANCEL THE CLOSE TOO
If MessageBox.Show("Do you want to Quit?", "Excel New Workbook", _
MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
'FALL THROUGH AND QUIT SILENTLY
Else
'CANCEL THE QUIT
e.Cancel = True
Exit Sub
End If
End If
End With
End If
'CLEAR OUR RESOURCES
oWB.Close(SaveChanges:=False)
oWB = Nothing
End If
oApp.Quit()
oApp = Nothing
Application.Exit()
End Sub
Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
'MAKE IT VISIBLE
oApp.Visible = True
'CREATE A NEW WORKBOOK BY ADDING IT TO THIS EXCEL APPLICATION INSTANCE
oWB = oApp.Workbooks.Add()
'NEEDED TO FOOL EXCEL IN OUR CLOSING PROCEDURE
oWB.Saved = False
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'CREATE THE NEW EXCEL APPLICATION INSTANCE
oApp = New Excel.Application
'MAKE IT INVISIBLE
oApp.Visible = False
End Sub
End Class
Attached Files
Last edited by RobDog888; Apr 1st, 2007 at 05:18 PM .
VB/Office Guru™ (AKA: Gangsta Yoda ™ ® )
I dont answer coding questions via PM. Please post a thread in the appropriate forum.
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it!
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6
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