VBA & Macro Problems

How to Cancel a VBA FileDialog Without an Error in WPS Office

John WilsonJohn Wilson Nov 13, 2026 869 views

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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In WPS Office, press Alt+F11 to open the VBA editor and open the module that contains the FileDialog macro.

2
Find the FileDialog and SelectedItems lines

Locate the code where the FileDialog is shown and where SelectedItems(1) is read.

3
Wrap the SelectedItems call in a return value check

Use: If dlg.Show = -1 Then selectedFile = dlg.SelectedItems(1) Else selectedFile = "" End If. Replace dlg and selectedFile with your actual variables.

4
Save and test the Cancel action

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.

Check the FileDialog.Show return value before reading SelectedItems(1)
Cancel is now handled safely: Adding the Show return value check prevents the Invalid procedure call or argument error and lets the macro continue cleanly after a cancellation.
WPS Office VBA

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. 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. 2. Store the Show result: Add result = dlg.Show before the line that reads SelectedItems(1).
  3. 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. 4. Save and run the macro: Save the VBA project, run the macro, and click Cancel to confirm no error appears.
Prevents Invalid procedure call or argument errors when users cancel a FileDialogMaintains compatibility with Microsoft Excel macro-enabled workbooks (.xlsm)Built-in VBA editor for editing and testing macros in WPS OfficeFree, lightweight Office suite with a familiar interface and file formats
microsoft office alternative - wps office

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.