vba 如何列出给定文件夹的所有文件(通过子文件夹递归)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2713838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:35:17  来源:igfitidea点击:

How to list all files of a given folder (recursively through sub-folder)?

vbams-word

提问by Nam G VU

I'm writing a microsoft word macro and having difficulty with vb.net. Please help.

我正在编写一个 microsoft word 宏并且在使用 vb.net 时遇到了困难。请帮忙。

采纳答案by MarkJ

Check out the answers here- VB6 is almost identical to VBA.

这里查看答案- VB6 几乎与 VBA 相同。

  • You will need to add a reference to the Microsoft Scripting Runtime if you want to useFileSystemObject
  • Or you can just drop inthe CDirDrill class which means you won't need any external references.
  • 如果要使用,则需要添加对 Microsoft Scripting Runtime 的引用FileSystemObject
  • 或者,你可以滴在了CDirDrill类,这意味着你将不再需要任何外部引用。

回答by Hans Olsson

A Word Macro is usually written in VBA rather than VB.Net, I'm assuming that you meant VBA?

Word 宏通常是用 VBA 而不是 VB.Net 编写的,我假设您的意思是 VBA?

Use the FileSystemObjectto access the filesystem and then use recursion to "walk" down. Here's a sample:

使用FileSystemObject访问文件系统,然后使用递归“走”下来。这是一个示例:

http://www.java2s.com/Code/VBA-Excel-Access-Word/File-Path/RecursiveSearchusingtheFileSystemObjectModel.htm

http://www.java2s.com/Code/VBA-Excel-Access-Word/File-Path/RecursiveSearchusingtheFileSystemObjectModel.htm

If it's actually VB.Net you're using, the idea will be the same but use System.IO.Directoryrather than the FileSystemObject.

如果它实际上是您正在使用的 VB.Net,想法将是相同的,但使用System.IO.Directory而不是FileSystemObject.

回答by user2780436

I personally like the FileSystemObject approach. i normally set up a recursive search function like the one found hereand have never had any problems.

我个人喜欢 FileSystemObject 方法。我通常会设置一个递归搜索函数,就像这里找到的那样,从来没有遇到过任何问题。