Excel File Doesn't Exist

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Perl Beginner
    New Member
    • Sep 2007
    • 57

    Excel File Doesn't Exist

    Hello all,
    I am new at VB6, but the more i learn, the more i love it! But i do have a question. I've created a form, the user has to choose an excel file from a list that they want to open, when they highlight the file name, the directory path shows up in another label box; then the user clicks the Open button to actually open the excel file for viewing. But what if the file doesn't exist yet? How do i add to my IF/Then statement a controlled message that tells the user that the file doesn't exist and to go back and select another file? Here is the code that i have so far:

    Code:
    Private Sub Command1_Click()
    Dim xl
    Dim xlsheet
    Dim xlwbook
    Dim MyFileName As String
    
    Me.Label3.ShowWhatsThis
    MyFileName = Me.Label3.Caption
    
    If Me.Label3.Caption <> "" Or Me.Label3.Caption <> ".xls" Then
    
    Set xl = CreateObject("Excel.Application")
    Set xlwbook = xl.Workbooks.Open(MyFileName)
    Set xlsheet = xlwbook.Sheets.Item(1)
    
    xl.Visible = True
    
    End If
    
    End Sub
    
    Private Sub Command2_Click()
    Unload Form3
    End Sub
    
    Private Sub Form_Load()
    List1.AddItem " BIO Only"
    List1.AddItem " CHEM Only"
    List1.AddItem " NUC Only"
    List1.AddItem " ROTA Only"
    End Sub
    
    Private Sub List1_Click()
    Label3.Caption = List1.Text
    Select Case List1.ListIndex
    Case 0
        Label3.Caption = "C:\file1.xls"
    Case 1
        Label3.Caption = "C:\file2.xls"
    Case 2
        Label3.Caption = "C:\file3.xls"
    Case 3
        Label3.Caption = "C:\file4.xls"
    End Select
    End Sub
    Thanks,
    Terra
  • kadghar
    Recognized Expert Top Contributor
    • Apr 2007
    • 1302

    #2
    Originally posted by Perl Beginner
    Hello all,
    I am new at VB6, but the more i learn, the more i love it! But i do have a question. I've created a form, the user has to choose an excel file from a list that they want to open, when they highlight the file name, the directory path shows up in another label box; then the user clicks the Open button to actually open the excel file for viewing. But what if the file doesn't exist yet? How do i add to my IF/Then statement a controlled message that tells the user that the file doesn't exist and to go back and select another file? Here is the code that i have so far:
    Thanks,
    Terra
    i only gave your code a fast glimpse.

    Why dont you try with an error handler?
    e.g.
    [CODE=vb]
    Sub MySub()
    On error goto ErrHandler
    'your code
    exit sub
    ErrHandler:
    msgbox "An error ocurried"
    end sub[/CODE]

    You can also check the Err object as well as it properties (number, description, etc) so you can give a "profession al look" to your error handler.
    Also try with another goto, or with a loop to retry loading a file if it wasnt found the first time.

    HTH

    Comment

    • Perl Beginner
      New Member
      • Sep 2007
      • 57

      #3
      Thank you so much HTH!! The error handler works great!
      Terra

      Comment

      Working...