2011-09-13 3 views
0

매크로 백그라운드에서 woorkbook을 열려고합니다. 사용자가 열린 파일 대화 상자를 종료하면 프로그램을 종료합니다.통합 문서 열기, 실패시 서브 종료/중단

그러나 그렇게 실패하고 모든 시도 ... 내가 지금까지 시도 무엇

' Get the file to open 
tempFile = Application.GetOpenFilename("Excel Files (*.xls), *.xls") 

: 난 항상 "형식 불일치"오류가

' Catch abort of the open file dialog 
If IsEmpty(tempFile) Then 
    End 
End If 
' Catch abort of the open file dialog 
If IsEmpty(tempFile) Or Not tempFile Then 
    End 
End If 
' Catch abort of the open file dialog 
If IsEmpty(tempFile) Or Not CBool(tempFile) Then 
    End 
End If 
' Catch abort of the open file dialog 
If IsEmpty(tempFile) Or tempFile Like "false" Then 
    End 
End If 

상관없이.

If tempFile = "False" Then 
: 당신은 아마 할 수있는 올바른 일이

dim tempFile as String 

을 ... 할 경우, 다음과 같은 검사를 할 필요가 있기 때문에

답변

4
dim tempFile 
tempFile = Application.GetOpenFilename("Excel Files (*.xls), *.xls") 
if tempFile = False then 
    'user cancelled the dialog. exit the code 
else 
    msgbox "User wants to open the file at : " & tempFile 
end if 
+0

추가적으로 http://msdn.microsoft.com/en-us/library/bb209892(v=office.12).aspx를 참조하십시오. 기사에 따르면 반환 유형은 변형입니다. – Fionnuala

-2

는 조심

곡물에 맞서지만 작동합니다.

+0

-1 아니, 그럴 필요가 없습니다. 할 수는 있지만 할 필요는 없습니다. 'Dim s As String : s = "False": Debug.Print s = False', 따옴표 사이에'False'를 적어 두십시오. 그리고'dim tempFile as String'은 "아마 올바른 일"이 아닙니다. 'GetOpenFilename'은'Variant'를 반환합니다. 원하는 경우 문자열로 강제 변환 할 수 있지만 괜찮 으면 좋을 것입니다. –