0

I am working to create it so when the application cannot find the file that the user selected, it will go back to the original form (where it was selected - frmLetters). So I am using a Try, Catch block but it just continues down the sub procedure.

I need it to stop the current sub procedure and go back to the original form for the user to choose the correct file. This is the code I am trying:

    Try
        objExcelBook = objExcelApp.Workbooks.Open(sInputExcelDataFile,, True)
    Catch ex As Exception
        MsgBox("Are You Sure you chose the correct file path?")
        Me.Close()
        frmLetters.Close()
        frmLetters.Show()
    End Try

I am much more familiar with VBA but the syntax is similar in most cases. I know try, Catch doesn't exist in VBA but if I wanted to return to the original form it is very simple.

Just so you know the reason I am closing frmLetters before opening it, is at this point it is only hidden.

djblois
  • 963
  • 1
  • 17
  • 52
  • Is your form lives in the MDIParent menu? – Yat Fei Leong Jul 26 '22 at 16:04
  • @YatFeiLeong yes it is in the parent Menu, – djblois Jul 26 '22 at 16:05
  • Instead of trying to open a file that might not exist in Excel, you can check if the file exists before opening it in Excel: [How to find out if a file exists in C# / .NET?](https://stackoverflow.com/questions/38960/how-to-find-out-if-a-file-exists-in-c-sharp-net). To go back to VBA, you could use [VBA check if file exists](https://stackoverflow.com/questions/16351249/vba-check-if-file-exists). – Andrew Morton Jul 26 '22 at 17:11

1 Answers1

0

First of all Create this method in your MDIParent. This should be the way that you display the form inside the MDI Parent. You should display your original form this way as well instead of using show.

Public Sub DisplaySubForm(ByVal f As Form)

        For Each currentForm As Form In Me.MdiChildren
            If f.GetType Is currentForm.GetType Then
                currentForm.Activate()
                Return
            End If
        Next
        f.MdiParent = Me
        f.Show()
        f.WindowState = FormWindowState.Normal
        f.BringToFront()

    End Sub

To Return the form, you simply can do like (you can do it on your form)

MDIParent1.DisplaySubForm(frmSomething)

To close the form, you use

me.dispose
Yat Fei Leong
  • 751
  • 1
  • 6
  • 10