0

我在幻灯片中插入了一个列表视图控件。在下一步中,我尝试使用以下代码显示所有文件和文件夹 + 图标:

For Each fileName In IO.Directory.GetFiles("C:\")
    ImageList21.Images.Add (Icon.ExtractAssociatedIcon(fileName))
    ListView31.Items.Add (system.IO.Path.GetFileName(fileName)) , _
        ImageList21.Images.Count - 1)
Next fileName

它在 Visual Studio 2008 上完美运行,但在 VBA(使用 VB6)上,我得到了很多错误,例如:

对于每个控制变量必须是变体或对象

还有,这行代码变红了,报错

ListView31.Items.Add (system.IO.Path.GetFileName(fileName)) , ImageList21.Images.Count - 1)

预期:语句结束

谁能解释如何将代码转换为 VB6/VBA?

4

2 回答 2

1

尝试使用 FileSystemObject。 在 MSDN 上

这是与您的 VB.NET 代码等效的 VBA(使用 FSO):

Dim fso, objFolder, objFile, strTemp
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder("c:\")
For Each objFile in objFolder.Files
    'might need some other VBA call to extract an icon from these files
    'ImageList21.Images.Add Icon.ExtractAssociatedIcon(objFile.Name)
    ListView31.Items.Add objFile.Name, ImageList21.Images.Count - 1)
Next
于 2012-09-18T18:54:42.100 回答
-2

这看起来像 VB.NET 代码。VBA 文件处理例程非常简单,但是您可以通过添加对它的引用从 VBA 调用 FileSystemObject。

您需要添加对“Microsoft Scripting Runtime”的引用。您可以在我相信的 VB 编辑器中执行此操作(已经有一段时间了)。

tgolisch 的代码示例应该是一个很好的起点。只要确保你有参考集。

于 2012-09-18T20:12:40.330 回答