如何将变量分配给 VBA 中的对象 属性

How to assign variable to Object Property in VBA

我正在尝试从 Shell.Application 对象动态设置 namespace 属性。

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

FolderNameFileName 的值固定(硬编码)时,代码段有效。但是当我尝试将它们作为变量传递时;错误 Run-time error 91 - Object variable or With Block variable not set. 正在返回。我还需要将返回值分配给变量以备后用; ExtendProperty。我对 VBA 有点陌生,在互联网上找不到任何关于这个特定案例的内容(范围、本地人等)。

为避免上述错误,将变量作为变体传递

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