How can I Open an Existing Workbook from my Procedure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sudheerudp
    New Member
    • Feb 2012
    • 1

    #1

    How can I Open an Existing Workbook from my Procedure

    How can I open an existing Workbook within my procedure?

    I need something to use instead of line #8.
    Code:
    Private Sub View_Record_Click()
    Dim objXL As Object
    Dim newbook As Excel.Workbook
    Dim newsht1 As Excel.Worksheet
    
    Set objXL = CreateObject(Class:="Excel.Application")
    
    Set newbook = objXL.Workbooks.Add 
    ' Here instead of add, i want add already existing worksheet
    
    Set newsht1 = newbook.Sheets("Sheet1")
    
    newsht1.Shapes.AddShape(msoShapeRectangle, 89.25, 72.75, 113.25, 92.25).Select
    objXL.Visible = True
    End Sub
    Last edited by NeoPa; Feb 7 '12, 03:41 PM. Reason: Added question in post and added [CODE] tags
  • MikeTheBike
    Recognized Expert Contributor
    • Jun 2007
    • 640

    #2
    Hi

    You have not indicated how/where you are going to determine the file path (or file name) but you can use the FileDialog object to 'pick' the file you want to open.

    Access help offers this bit of code which, with a little research, will enable you to obtain a file path/name by selecting it.
    Code:
    Dim dlgOpen As FileDialog
    
    Set dlgOpen = Application.FileDialog( _
        FileDialogType:=msoFileDialogOpen)
    
    With dlgOpen
        .AllowMultiSelect = True
        .Show
    End With
    HTH


    MTB

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32669

      #3
      You may find the following articles helpful :
      1. Select a File or Folder using the FileDialog Object.
      2. Application Automation.


      A helps with identifying the file, and B helps with opening a workbook file (GetObject()).

      Plase also check out Before Posting (VBA or SQL) Code.

      Comment

      Working...