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


2

我在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

我唯一要改变的就是论证 .GetDetailsOf 方法,从常量切换到变量。每当它运行时,它就会停在该行上,并显示“错误445:对象不支持此操作”

我究竟做错了什么?


尝试传球 strPath & strFile 到GetTags而不仅仅是strFile。我的意思是你可以将它作为文字串联传递,你不一定要将它作为两个参数传递。
Bill Hileman

Answers:


1

编辑:

好。仍然无法确切地解决为什么案例2不起作用,但我 发现以“正确”的方式获取FolderItem对象所对应的 strFile (根据要求) .GetDetailsOf() )是用的 .ParseName() 方法:

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

我无法解释 为什么 它不起作用,但我确实有三个解决方法。


1)使用 CStr(strFile) 代替 strFile 在打电话时 .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

要么

2)更改参数类型 strFileVariant

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

要么

3)将空字符串连接到 strFile 在打电话时 .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

好的,那只是......疯了。为什么会有用?!它更接近了,但仍然有些东西......而不是返回“导入188”,就像我使用常量时那样,它返回“标签”,类别的名称而不是其内容。
Werrf

@Werrf增加了第三种解决方法。我仍然感到困惑的是它为什么不起作用,甚至为什么解决方案甚至设法绕过错误。
robinCTS

@Werrf不确定你的做法有何不同,但所有三种解决方法都在我的工作中。不过,我正在运行Excel 2007。
robinCTS

得到它 - 我试过传递完整的路径 strFile 看看是否有所作为。当我通过完整的道路时,它给了我 Tags 结果,但当我传递它只是文件名时,它给了我标签的内容, Imported 188。工作,谢谢!
Werrf

@Werrf我想我知道问题是什么。你应该使用 strFile 代替 .Items.Item(strFile) 在打电话时 .GetDetailsOf()。仍然试图确定准确的细节,但似乎 .Items 没有“适当的” .Item 方法有时会失败。您可以使用FolderItem对象或字符串作为参数 .GetDetailsOf 所以使用字符串应该可以解决问题。我可能会尝试更多的研究,然后更新我的答案。
robinCTS

-1

Dir()函数不返回完整路径,它只返回文件名和扩展名。因此,当您尝试访问标记时,如果获得Dir()的结果,则只传递文件名和扩展名。相反,预先挂起路径,如下所示。我在GetTags中更改了传递变量的名称,以帮助避免混淆。

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(strPath & strFile)
        strFile = Dir()
    Loop
End Sub

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

没变。请注意,在有效的代码中,常量也只包含文件名。
Werrf

在您的代码中,您可以定义 csFile 但你永远不会使用我能看到的 - 它仍然引用仅在TestFlags()中声明的strFile。你还声明了一个永远不会被使用的整数i。
Bill Hileman

这是我的疏忽,对不起 - 当我复制它时,我一定错过了这个改变。已更正以显示工作代码使用 csFile 在第一个参数中 .GetDetailsOf。整数i是我正在进行的早期测试中的人工制品,可以忽略。
Werrf
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.