Shell.Application Weird Behavior: Prevent Debugging Headaches Now!

Shell App Weird Behaviour

Exploring VBA (Visual Basic for Applications) can sometimes feel like finding your way through a complex puzzle, especially when working with the Microsoft Shell Controls and Automation reference. In this blog post, we’ll explore an unusual behavior.

Problem:

We’ve noticed that Shell.App throwing an error in case of using string variable in late binding. That might puzzle even experienced VBA users.

Test Case:

To taste the issues we have tried to count how many files are there in a specific folder path using ShellApp.

If you prefer to see a full video of this issue, then you can check this video link here:- Shell.Application Trap Exposed.

Primarily we have run the test using early binding method and it works correctly in both cases whether it is using Constant to declare the file path or a string Variable.
As usual, it prints the correct result in both cases.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Private Sub TestEarlyBindingConst()
     
    Const TEST_FOLDER As String = "D:\Downloads"
     
    Dim ShellApp As Shell32.Shell
    Set ShellApp = New Shell32.Shell
     
    Dim SourceItems As Shell32.FolderItems
    Set SourceItems = ShellApp.Namespace(TEST_FOLDER).Items
    Debug.Print SourceItems.Count
     
End Sub
 
Private Sub TestEarlyBindingVar()
     
    Dim TEST_FOLDER As String
    TEST_FOLDER = "D:\Downloads"
     
    Dim ShellApp As Shell32.Shell
    Set ShellApp = New Shell32.Shell
     
    Dim SourceItems As Shell32.FolderItems
    Set SourceItems = ShellApp.Namespace(TEST_FOLDER).Items
    Debug.Print SourceItems.Count
     
End Sub

Then we have tried the same test using late binding. And in case of Constant, it works correctly.

1
2
3
4
5
6
7
8
9
10
11
12
Private Sub TestLateBoundConst()
     
    Const TEST_FOLDER As String = "D:\Downloads"
     
    Dim ShellApp As Object
    Set ShellApp = CreateObject("Shell.Application")
     
    Dim SourceItems As Object
    Set SourceItems = ShellApp.Namespace(TEST_FOLDER).Items
    Debug.Print SourceItems.Count
     
End Sub

But when we run the test where we have declared the folder path as a string variable and then use the variable, it throws an error.

1
2
3
4
5
6
7
8
9
10
11
12
13
Private Sub TestLateBoundVar()
     
    Dim TEST_FOLDER As String
    TEST_FOLDER = "D:\Downloads"
     
    Dim ShellApp As Object
    Set ShellApp = CreateObject("Shell.Application")
     
    Dim SourceItems As Object
    Set SourceItems = ShellApp.Namespace(TEST_FOLDER).Items
    Debug.Print SourceItems.Count
     
End Sub

The Error is

Error

And the issue is on that line of code.

But the same code works perfectly in the case of constant.

Findings:

So we have gone through a trial and error process and found a solution to this weird behavior where we have used empty string before and after the TEST_FOLDER parameter Or vbNullString. Then the code runs perfectly.

1
2
3
4
5
6
7
8
9
10
11
12
13
Private Sub TestLateBoundVarWithEmptyString()
     
    Dim TEST_FOLDER As String
    TEST_FOLDER = "D:\Downloads"
     
    Dim ShellApp As Object
    Set ShellApp = CreateObject("Shell.Application")
     
    Dim SourceItems As Object
    Set SourceItems = ShellApp.Namespace("" & TEST_FOLDER & "").Items
    Debug.Print SourceItems.Count
     
End Sub

We think you may have noticed this weird behavior of the Microsoft Shell Controls and Automation reference.

We’ve set out to explore the unusual aspects of late binding with the Microsoft Shell Controls And Automation reference in VBA. By persistently investigating and staying determined, we’ve discovered valuable insights that will surely enhance our coding skills in the future.

You are welcomed to share your thoughts about this post in the comment section.

Leave a Reply

Subscribe to Blog via Email

Enter your email to subscribe to this blog and receive notifications of new posts by email.

You may also like

Please login to Continue Reading