使用可变文件名在VBA中获取文件标签

我在Excel中使用VBA遍历一系列文件并确定要导入的文件。我想使用文件标签之类的内容来决定要导入的文件,这样就不必打开每个文件了。我正在尝试使用该GetDetailsOf方法来获取它们,但是当我尝试使用变量作为文件名时却失败了。

此代码使用常量作为文件名,可以正常工作:

Sub TestTags()
  Dim strPath As String
  Dim strFile As String

  strPath = "C:\Users\XXXX\Documents\Safe Space\MacroTest\"
  strFile = Dir(strPath & "*.xls*")
  Do While strFile <> ""
      Debug.Print GetTags()
      strFile = Dir()
  Loop
End Sub

Function GetTags()
  Const csFile As String = "MyTestFile.xlsx"

  With CreateObject("Shell.Application").Namespace("C:\Users\XXXX\Documents\Safe Space\MacroTest\")
      GetTags = .GetDetailsOf(.Items.Item(csFile), 18)
  End With
End Function

但是,当我尝试用调用子例程传递的变量替换常量时,出现错误。这是失败的代码:

Sub TestTags()
    Dim strPath As String
    Dim strFile As String

    strPath = "C:\Users\XXXX\Documents\Safe Space\MacroTest\"
    strFile = Dir(strPath & "*.xls*")
    Do While strFile <> ""
        Debug.Print GetTags(strFile)
        strFile = Dir()
    Loop
End Sub

Function GetTags(ByVal strFile As String)
    Const csFile As String = "MyTestFile.xlsx"
    Dim i As Integer

    With CreateObject("Shell.Application").Namespace("C:\Users\XXXX\Documents\Safe Space\MacroTest\")
        GetTags = .GetDetailsOf(.Items.Item(strFile), 18)
    End With
End Function

The only thing I'm changing is the argument in the .GetDetailsOf method, switching from a constant to a variable. Whenever it runs, it stops on that line with 'Error 445: Object doesn't support this action'

What am I doing wrong?

robinCTS

EDIT:

OK. Still can't work out precisely why case 2 doesn't work, but I have found that the "proper" way to get the FolderItem object corresponding to strFile (as required by .GetDetailsOf()) is to use the .ParseName() method:

Function GetTags(ByVal strFile As String)
    Const csFile As String = "MyTestFile.xlsx"
    Dim i As Integer

    With CreateObject("Shell.Application").Namespace("C:\Users\XXXX\Documents\Safe Space\MacroTest\")
        GetTags = .GetDetailsOf(.ParseName(strFile)), 18)
    End With
End Function

I can't explain why it doesn't work, but I do have three work-arounds.


1) Use CStr(strFile) instead of strFile when calling .GetDetailsOf():

Function GetTags(ByVal strFile As String)
    Const csFile As String = "MyTestFile.xlsx"
    Dim i As Integer

    With CreateObject("Shell.Application").Namespace("C:\Users\XXXX\Documents\Safe Space\MacroTest\")
        GetTags = .GetDetailsOf(.Items.Item(CStr(strFile)), 18)
    End With
End Function

or

2) Change the parameter type of strFile to Variant:

Function GetTags(ByVal strFile As Variant)
    Const csFile As String = "MyTestFile.xlsx"
    Dim i As Integer

    With CreateObject("Shell.Application").Namespace("C:\Users\XXXX\Documents\Safe Space\MacroTest\")
        GetTags = .GetDetailsOf(.Items.Item("" & strFile), 18)
    End With
End Function

or

3) Concatenate a null string to strFile when calling .GetDetailsOf():

Function GetTags(ByVal strFile As Variant)
    Const csFile As String = "MyTestFile.xlsx"
    Dim i As Integer

    With CreateObject("Shell.Application").Namespace("C:\Users\XXXX\Documents\Safe Space\MacroTest\")
        GetTags = .GetDetailsOf(.Items.Item("" & strFile), 18)
    End With
End Function

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用Lua从URL获取文件名

来自分类Dev

如何使用Java在Windows中获取短文件名?

来自分类Dev

获取目录路径中的文件名

来自分类Dev

获取特征中的类文件名

来自分类Dev

在Dynamics Ax中获取文件名

来自分类Dev

使用VBA从文件名中提取长度可变的字符串

来自分类Dev

在SaveAs窗口中设置Excel文件的文件名,而在vba中不使用sendkey

来自分类Dev

Python,循环使用文件名的可变部分

来自分类Dev

如何在目录中使用文件名python中的最大数量获取文件名?

来自分类Dev

如何使用find命令获取cygwin中的文件名?

来自分类Dev

文件名的R个可变部分

来自分类Dev

在javascript中获取文件名

来自分类Dev

在JavaScript中获取文件名

来自分类Dev

从HTTP帖子中获取文件名

来自分类Dev

从PHP中的URL获取文件名

来自分类Dev

在Erlang中从IoDevice获取文件名

来自分类Dev

使用批处理脚本在目录中获取文件名

来自分类Dev

如何从具有可变文件名的查找中获取文件路径

来自分类Dev

从Bash中的目录获取文件名

来自分类Dev

获取目录中的文件名

来自分类Dev

使用VBA删除公式中对文件名的引用

来自分类Dev

如何使用PowerShell中的文件内容获取文件名

来自分类Dev

如何使用jQuery从输入类型文件中获取选定的文件名和文件路径

来自分类Dev

在 openfiledialog 中获取文件名

来自分类Dev

在excel中根据变量文件名VBA打开文件

来自分类Dev

使用vim获取目录中的所有文件名

来自分类Dev

获取在 setGraphic() 上使用的文件名

来自分类Dev

使用powershell在linux发行版中获取文件名和文件扩展名

来自分类Dev

使用powershell从文件名中获取时间戳

Related 相关文章

热门标签

归档