Fix Access Denied When Office VBA Runs the DIR Command
Question details
Resolve an access denied error when Office VBA runs the DIR command through WScript.Shell.

- Product
- Microsoft Office VBA and Microsoft Defender
- Device & OS
- not provided
- Scenario
- A user wants an Office macro to list directory contents by launching a command shell.
- Observed behavior
- WScript.Shell returns error 2147024891 (80070005), even though DIR works in Command Prompt.
Identify the Office application, the folder being listed, and the exact VBA line that raises the error. If the device is managed by an organization, ask its IT team to review security policy events before changing any settings.
Check Microsoft Defender rules that block Office child processes
Use this approach when DIR works in Command Prompt but launching a shell from Office is denied.
Security baselines or Microsoft Defender attack surface reduction rules can restrict processes started by Office. A successful interactive Command Prompt test does not prove that Office is allowed to start the same process.
Open Event Viewer and go to Applications and Services Logs > Microsoft > Windows > Windows Defender > Operational. Check events around the time the macro failed for a blocked Office process or attack surface reduction rule.
Ask your administrator to inspect the device's Microsoft Defender or security baseline policy, especially the rule that blocks Office applications from creating child processes. Use the event details to identify the rule involved rather than changing unrelated protections.
If the administrator confirms a policy is responsible, have them consider setting that specific rule to audit mode or applying an approved exception according to organizational policy. Run the macro again and review the Defender events.

List files with FileSystemObject instead of launching DIR
Use VBA's FileSystemObject when the goal is to enumerate files and a command shell is unnecessary.
Test the target folder and shell command
Use a simple test to distinguish folder permissions or command construction from an Office process restriction.
Work with Office Documents in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface and support for common Microsoft Office formats, making it easier to move everyday document work. It does not change Microsoft Defender policies or fix an Office VBA security restriction.

Frequently Asked Questions
Why does DIR work in Command Prompt but fail from an Office macro?
A security rule may allow an interactive Command Prompt while blocking an Office application from creating a child process. Check Windows Defender's Operational log in Event Viewer for events at the time of the failure.
What does VBA error 80070005 mean?
Error 80070005 means access was denied. In this scenario, the denial may involve the target folder, the process launch, or an applicable security policy; review the failing line and Defender events to narrow it down.
Can VBA list files without running CMD?
Yes. Create a Scripting.FileSystemObject, call GetFolder with the target path, and loop through the folder's Files collection. The account still needs permission to read that folder.
Should I disable all Office protection rules to run the macro?
No. Ask an administrator to identify the specific rule in the Defender event log and assess an approved audit-mode test or exception. Changing unrelated rules can reduce security without fixing the issue.




