2011-09-13 4 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 

、どんな。また

答えて

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

、あなたはおそらく行うには正しいことだ

dim tempFile as String 

を...行う場合は、あなたがこのようなチェックをしなければならないので注意してください:

If tempFile = "False" Then 

は穀物に逆らっていますが、機能します。

+0

-1いいえ、あなたは必要ありません。あなたはすることができますが、する必要はありません。 'Dim s As String:s =" False ":Debug.Print s = False'、' False'は引用符で囲まれていないことに注意してください。そして 'dim tempFile as String'は"おそらく正しいこと "ではありません。 'GetOpenFilename'は' Variant'を返します。あなたは望むならそれを文字列に強制することができますが、これはうまくいきますが、それはおそらくより正確になりません。 –

関連する問題