Private Sub Command1_Click() On Error GoTo nofile With CommonDialog1 .InitDir = "c:\windows" .Filter = "*.txt" .CancelError = True .ShowOpen End With Label1.Caption = CommonDialog1.FileName Exit Subnofile: If Err.Number = 32755 Then Label1.Caption = "放弃操作" Exit Sub Else Label1.Caption = "其它错误" End If End Sub
On Error GoTo nofile
With CommonDialog1
.InitDir = "c:\windows"
.Filter = "*.txt"
.CancelError = True
.ShowOpen
End With
Label1.Caption = CommonDialog1.FileName
Exit Subnofile:
If Err.Number = 32755 Then
Label1.Caption = "放弃操作"
Exit Sub
Else
Label1.Caption = "其它错误"
End If
End Sub
关键是CancelError 属性,你按了取消按钮后会产生一个可捕获的错误