# Avoid an Error Message when Importing

Consider the following code

Button["Import", file = SystemDialogInput["FileOpen"];
data = Import[file], Method -> "Queued"]


which yields a button which, upon clicking, opens a directory window from which I can access my files and import one. However, in Windows 10, if I click "Cancel", that is,

I get back the following message, regarding $Canceled, How do I avoid such a message? That is, if I click "Cancel" I want both file and data to remain non attributed. I thought about simply checking whether data or file are Unevaluated[Sequence[]], but not sure how to do this or if there is a better way of doing it. Any ideas? ## 1 Answer I'm new and this is my 1st answer. Try the following: Button["Import", file = SystemDialogInput["FileOpen"]; If[ Not@TrueQ[file ==$Canceled], data = Import[file]],
Method -> "Queued"]

• That works, thank you!! Mar 16, 2020 at 22:00