Results 1 to 1 of 1

Thread: [FAQ's: OD] How do I open an existing Excel 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 open an existing Excel Workbook?

    If you need to programmatically open an existing workbook from within Excel itself you would use the Application.Workbooks.Open function. Now if you are automating Excel from another program language like VB 6 or a .NET language then you would create the application instance, select the Excel workbook and call the .Open function on the selected workbook.


    Excel 97 - 2003 and VB.NET 2003 - 2008

    VB.NET Code:
    1. Option Explicit On
    2. Option Strict On
    3. 'Copyright © 2009 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.     'Declare our Excel Application object and Workbook object
    15.     Private moApp As Excel.Application
    16.     Private moWB As Excel.Workbook
    17.  
    18.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    19.         'Open a selected workbook (*.xls)
    20.         Dim OpenDlg As New OpenFileDialog
    21.         Using OpenDlg
    22.             With OpenDlg
    23.                 .CheckFileExists = True
    24.                 .CheckPathExists = True
    25.                 .Filter = "ExcelWorkbooks Only (*.xls)|*.xls"
    26.                 .FilterIndex = 1
    27.                 .Multiselect = False
    28.                 .ShowHelp = False
    29.                 .ShowReadOnly = False
    30.                 .Title = "Select an Excel Workbook file"
    31.                 'If a file is selected then lets open it
    32.                 If .ShowDialog = Windows.Forms.DialogResult.OK Then
    33.                     'Call the .Open function and assign the return to the workbook variable for use
    34.                     'in other functions/areas in our app
    35.                     moWB = moApp.Workbooks.Open(Filename:=.FileName)
    36.                     'Show the opened workbook/application
    37.                     moApp.Visible = True
    38.                 End If
    39.             End With
    40.         End Using
    41.     End Sub
    42.  
    43.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    44.         'Close and dispose our objects
    45.         If TypeName(moWB) <> "Nothing" Then
    46.             'Can change to autosave or not
    47.             moWB.Close() 'SaveChanges:=True
    48.         End If
    49.         moWB = Nothing
    50.         If TypeName(moApp) <> "Nothing" Then
    51.             moApp.Quit()
    52.         End If
    53.         moApp = Nothing
    54.     End Sub
    55.  
    56.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    57.         'Error handling on creating an Excel application as the user may not have it installed
    58.         Try
    59.             moApp = DirectCast(CreateObject("Excel.Application"), Excel.Application)
    60.         Catch ex As Exception
    61.             MessageBox.Show(ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    62.         End Try
    63.     End Sub
    64.  
    65. End Class
    Last edited by RobDog888; Jan 10th, 2009 at 05:13 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