Search code examples
vbaexcelole

How to assign variable to Object Property in VBA


I'm trying to set namespace property from Shell.Application object dynamically.

With CreateObject("Shell.Application").Namespace(FolderName)
    ExtendProperty = .GetDetailsOf(.Items.Item(FileName), 143)
End With

When values for FolderName and for FileName are fixed(hard coded) snippet is working. But when I try to pass them in as a variables; error Run-time error 91 - Object variable or With Block variable not set. is returning. Also I need to assign returned value to variable for later use; ExtendProperty. I'm a bit new to VBA and could not find anything on the internet(Scopes, locals, etc.) for this specific case.


Solution

  • To avoid the error mentioned pass variables as variants

    Sub TEST2()
        Dim ExtendProperty As Variant
        Dim folderName As Variant, FileName As Variant
        folderName = "C:\Users\User\Desktop\TestFolder"
        FileName = "Test.xlsx"
        With CreateObject("Shell.Application").Namespace(folderName)
           ExtendProperty = .GetDetailsOf(.Items.item(FileName), 143)
        End With
    End Sub