How to Cancel a VBA FileDialog Without an Error in WPS Office
Question details
The user needs to let a VBA FileDialog close safely when the user clicks Cancel instead of raising an error when the macro reads SelectedItems(1).
- Product
- WPS Office
- Device & OS
- not provided
- Scenario
- A VBA macro opens a FileDialog for the user to choose a file, but the user may click Cancel.
- Observed behavior
- The macro raises an Invalid procedure call or argument error because it tries to access SelectedItems(1) after the dialog is canceled, when no item is selected.
Open the VBA editor in WPS Office and identify the macro that creates the FileDialog. Confirm that macros are enabled so you can run and test the change after editing.
Check the FileDialog.Show return value before reading SelectedItems(1)
Use this solution when the macro must handle both file selection and cancellation without raising an error.
The FileDialog.Show method returns -1 when the user selects a file and 0 when the user clicks Cancel. After cancel, SelectedItems(1) does not exist, so accessing it causes an Invalid procedure call or argument error.
In WPS Office, press Alt+F11 to open the VBA editor and open the module that contains the FileDialog macro.
Locate the code where the FileDialog is shown and where SelectedItems(1) is read.
Use: If dlg.Show = -1 Then selectedFile = dlg.SelectedItems(1) Else selectedFile = "" End If. Replace dlg and selectedFile with your actual variables.
Save the macro, run it, and click Cancel in the file dialog. The macro should now return an empty string or continue without an error.

Exit the macro or show a cancellation message when the dialog returns 0
Use this alternative when you want the macro to stop immediately or inform the user that no file was selected.
Fix VBA FileDialog Cancel Errors in WPS Office
Edit the macro in WPS Office to check the FileDialog.Show return value before reading SelectedItems(1). WPS Office includes a built-in VBA editor, so you can open the macro, update the code, save, and test the Cancel action directly.
- 1. Open the macro in WPS Office: In WPS Office, press Alt+F11 to open the VBA editor and locate the macro that calls FileDialog.
- 2. Store the Show result: Add result = dlg.Show before the line that reads SelectedItems(1).
- 3. Check the result and return an empty string on cancel: Use: If result = -1 Then selectedFile = dlg.SelectedItems(1) Else selectedFile = "" End If.
- 4. Save and run the macro: Save the VBA project, run the macro, and click Cancel to confirm no error appears.

Frequently Asked Questions
Why does my VBA FileDialog throw an Invalid procedure call or argument error when I click Cancel?
The error occurs because the code tries to access SelectedItems(1) after cancellation. When the dialog is canceled, no item is selected, so SelectedItems(1) does not exist. Check the return value of .Show first; it returns 0 for Cancel and -1 after a file selection.
What value does FileDialog.Show return in WPS Office VBA?
FileDialog.Show returns -1 when the user selects a file and 0 when the user clicks Cancel. You can store this result in a variable and use it to decide whether to read SelectedItems(1).
How do I return an empty string when no file is selected in a WPS Office macro?
Use an If block: If dlg.Show = -1 Then selectedFile = dlg.SelectedItems(1) Else selectedFile = "" End If. Replace dlg and selectedFile with your variable names.
Does this FileDialog cancel fix work for Microsoft Excel macro files opened in WPS Office?
Yes. WPS Office supports VBA macros and macro-enabled Excel workbook formats such as .xlsm. The same return value check applies to FileDialog operations in compatible VBA projects.
Can I use Application.FileDialog in WPS Office VBA?
Yes, WPS Office VBA supports Application.FileDialog. The key is to handle its Show return value correctly so Cancel does not cause an error when SelectedItems is read.




