2013-09-28 62 views
0

我正在從Excel打開文件的過程。我想插入一個檢查,如果用戶按下打開沒有選擇一個文件,然後有消息框彈出警告。OpenFileDialog - 檢查是否未選擇

這是我的代碼的一部分,我想插入檢查。我嘗試使用Is Nothing,但它不適合我。

  If GetOpenFileName.ShowDialog() = System.Windows.Forms.DialogResult.OK Then 
       fileStream = GetOpenFileName.OpenFile() 

       If (fileStream Is Nothing) Then 'I tried checking here, but it does not fire. 
        vmbContinue = MsgBox(strAlert, MsgBoxStyle.RetryCancel + MsgBoxStyle.Critical, "No Workbook Seletected") 

        If vmbContinue = MsgBoxResult.Cancel Then 
         xlWB.Close(SaveChanges:=False) 

         Exit Sub 

回答

1

不知道你到底在問什麼,但是這是我打開文件時使用的代碼,如果他們不選擇一個,它不會讓他們。

Dim ofd As New OpenFileDialog 
    With ofd 
     'select the atributes before opening 
     'the filename (will show in the filename box) 
     .FileName = "" 
     'the title of the window to open a file 
     .Title = "Open File..." 
     'the extension filter 
     .Filter = "Exel Files|*.exel" 
     ''now to open the dialogue and check if OK has been pressed 
     If .ShowDialog = Windows.Forms.DialogResult.OK Then 
      MsgBox("File Opened Sucessfully") 
      'put the code here when they choose a file 
     Else 
      MsgBox("Please Select A File") 
      'put the code here if they click cancel or close 
     End If 
    End With