Results 1 to 3 of 3

Thread: [FAQ's: OD] How do I create a new workbook?

  1. #1

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    [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:
    1. Option Explicit
    2. 'Behind "ThisWorkbook" class
    3. Public Sub AddNewWorkbook()
    4.     'Define a workbook variable in case you want to perform any modifications on the workbook when it is created
    5.     Dim oWB As Excel.Workbook
    6.     'Add it to the Workbooks collection and set our object variable equal to the newly added workbook
    7.     'By referencing the "Application" object instance we are adding the workbook to the current
    8.     'runnng Excel instance (since we are in VBA).
    9.     Set oWB = Application.Workbooks.Add
    10.     'Perform and mods to the new workbook.
    11.     '...
    12.     'Close the new workbook, Save it and clean up resources
    13.     oWB.Close SaveChanges:=True, FileName:="C:\NewWorkbook.xls"
    14.     Set oWB = Nothing
    15. End Sub

    Then to call the procedure from any sheet or module ....
    VB Code:
    1. Option Explicit
    2. 'Behind Sheet1
    3. Sub Button1_Click()
    4.     AddNewWorkbook
    5. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  2. #2

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [FAQ's: OD] How do I create a new workbook?

    VB 6 and Excel 2003 Early Binding Code Example:
    VB Code:
    1. Option Explicit
    2. 'Add a reference to MS Excel xx.0 Object Library
    3. Private Sub Command1_Click()
    4.     Dim oApp As Excel.Application
    5.     Dim oWB As Excel.Workbook
    6.    
    7.     'Create an Excel instance.
    8.     Set oApp = New Excel.Application
    9.  
    10.     'Create a new workbook
    11.     Set oWB = oApp.Workbooks.Add
    12.  
    13.     'Perform and mods to the new workbook.
    14.     '...
    15.  
    16.     'Close the new workbook, Save it and clean up resources
    17.     oWB.Close SaveChanges:=True, FileName:="C:\NewWorkbook.xls"
    18.     Set oWB = Nothing
    19.     'Quit/Close the Excel application
    20.     oApp.Quit
    21.     Set oApp = Nothing
    22. End Sub
    VB 6 and Excel 2003 Late Binding Code Example:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Dim oApp As Object
    5.     Dim oWB As Object
    6.    
    7.     'Create an Excel instance.
    8.     Set oApp = CreateObject("Excel.Application")
    9.  
    10.     'Create a new workbook
    11.     Set oWB = oApp.Workbooks.Add
    12.  
    13.     'Perform and mods to the new workbook.
    14.     '...
    15.  
    16.     'Close the new workbook, Save it and clean up resources
    17.     oWB.Close SaveChanges:=True, FileName:="C:\NewWorkbook.xls"
    18.     Set oWB = Nothing
    19.     'Quit/Close the Excel application
    20.     oApp.Quit
    21.     Set oApp = Nothing
    22. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  3. #3

    Thread Starter
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [FAQ's: OD] How do I create a new workbook?

    VB.NET 2003 And Excel 2003 Early Binding Code Example:



    VB Code:
    1. Option Explicit On
    2. Option Strict On
    3. 'Copyright © 2006 by RobDog888 (VB/Office Guru™). All Rights reserved.
    4. '
    5. '              You may not reproduce or publish this
    6. '              code on any web site, online service,
    7. '              or distribute as source on any media
    8. '              without express permission.
    9.  
    10. 'Add a reference to Microsoft Excel xx.0 Object Library
    11. Imports Microsoft.Office.Interop
    12.  
    13. Public Class Form1
    14.  
    15.     Inherits System.Windows.Forms.Form
    16.  
    17.     "Windows Form Designer generated code"
    18.  
    19.     Private oApp As Excel.Application
    20.     Private oWB As Excel.Workbook
    21.  
    22.     Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
    23.         Me.Close()
    24.     End Sub
    25.  
    26.     Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
    27.     Handles MyBase.Closing
    28.         'CHECK IF THE WORKBOOK WAS SAVED FROM THE USER IN EXCEL
    29.         If TypeName(oWB) <> "Nothing" Then
    30.             If oWB.Saved = False Then
    31.                 Dim dlgSaveFile As New SaveFileDialog
    32.                 With dlgSaveFile
    33.                     .CheckFileExists = True
    34.                     .CheckPathExists = True
    35.                     .Filter = "Excel Files Only (*.xls)|*.xls"
    36.                     .FilterIndex = 0
    37.                     .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
    38.                     .CreatePrompt = True
    39.                     .FileName = "Book1.xls"
    40.                     .OverwritePrompt = True
    41.                     .ValidateNames = True
    42.                     .Title = "Select an Excel Workbook File"
    43.                     If .ShowDialog = DialogResult.OK Then
    44.                         oWB.SaveAs(FileName:=.FileName)
    45.                         oWB.Saved = True
    46.                     Else
    47.                         'CANCECL WAS PRESSED SO PROMPT TO CANCEL THE CLOSE TOO
    48.                         If MessageBox.Show("Do you want to Quit?", "Excel New Workbook", _
    49.                         MessageBoxButtons.OKCancel, MessageBoxIcon.Question) = DialogResult.OK Then
    50.                             'FALL THROUGH AND QUIT SILENTLY
    51.                         Else
    52.                             'CANCEL THE QUIT
    53.                             e.Cancel = True
    54.                             Exit Sub
    55.                         End If
    56.                     End If
    57.                 End With
    58.             End If
    59.             'CLEAR OUR RESOURCES
    60.             oWB.Close(SaveChanges:=False)
    61.             oWB = Nothing
    62.         End If
    63.         oApp.Quit()
    64.         oApp = Nothing
    65.         Application.Exit()
    66.     End Sub
    67.  
    68.     Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddNew.Click
    69.         'MAKE IT VISIBLE
    70.         oApp.Visible = True
    71.         'CREATE A NEW WORKBOOK BY ADDING IT TO THIS EXCEL APPLICATION INSTANCE
    72.         oWB = oApp.Workbooks.Add()
    73.         'NEEDED TO FOOL EXCEL IN OUR CLOSING PROCEDURE
    74.         oWB.Saved = False
    75.     End Sub
    76.  
    77.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
    78.         'CREATE THE NEW EXCEL APPLICATION INSTANCE
    79.         oApp = New Excel.Application
    80.         'MAKE IT INVISIBLE
    81.         oApp.Visible = False
    82.     End Sub
    83.  
    84. End Class
    Attached Files 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width