VBA & Macro Problems

Fix Access Denied When Office VBA Runs the DIR Command

Rana GarciaRana Garcia Nov 18, 2026 869 views

Question details

Resolve an access denied error when Office VBA runs the DIR command through WScript.Shell.

Fix Access Denied When Office VBA Runs the DIR Command
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.
Before you start

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.

Solution 1Recommended

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.

1
Review Defender events

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.

2
Identify the policy

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.

3
Test an approved policy change

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.

Check Microsoft Defender rules that block Office child processes
Keep security controls in mind: Audit mode records activity without enforcing the rule. Disabling or relaxing a protection reduces security and should be an administrator-approved decision.
Free Microsoft Office alternative

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.

Open and edit common Microsoft Word, Excel, and PowerPoint formatsUse a familiar interface for everyday document, spreadsheet, and presentation workWork with spreadsheets without relying on this particular DIR shell commandMove existing Office files into a lightweight, free office suite
microsoft office alternative - wps office

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.