James Banks
James Banks

Reputation: 1

Double openDialog VBA

When I try to run this code it opens an open dialog with no results, then it opens another and does what I want it to do. Help?

Private Sub mnuOpen_Click(sender As Object, e As EventArgs) Handles mnuOpen.Click
    Dim DidWork As Integer = openFD.ShowDialog()
    openFD.InitialDirectory = "C:\"
    openFD.Title = "Open a text file"
    openFD.Filter = "Text Files|*.txt|Word Docs|*.doc"
    openFD.ShowDialog()
    If DidWork = DialogResult.Cancel Then
        MsgBox("Cancel Button Clicked")
    Else
        strFileName = openFD.FileName
        MsgBox(strFileName)
    End If
End Sub

Upvotes: 0

Views: 74

Answers (1)

LarsTech
LarsTech

Reputation: 81610

You don't need the DidWork variable in your example (and as pointed out, you are calling ShowDialog twice).

I would favor checking for Ok instead of Cancel in the DialogResult:

openFD.InitialDirectory = "C:\"
openFD.Title = "Open a text file"
openFD.Filter = "Text Files|*.txt|Word Docs|*.doc"
If openFD.ShowDialog() = DialogResult.Ok Then
  strFileName = openFD.FileName
  MsgBox(strFileName)
Else
  MsgBox("Dialog Canceled")
End If

Upvotes: 1

Related Questions