ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
楼主: 三戒

[原创] 在office 2007中增加FileSearch对象[2009.05.23更新]

  [复制链接]

TA的精华主题

TA的得分主题

发表于 2009-12-18 15:34 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
支持支持

TA的精华主题

TA的得分主题

发表于 2010-1-22 00:55 | 显示全部楼层
感谢三戒高手提供的好东西,我在国外的好多Excel论坛上都有人抱怨这个问题,我试了很多他们提供的代码,这个是比较经典的一个。转过来大家一起享受!

Excel
VBA
Application.FileSearch
2003
2007

'!!!! Replacement solution including searching in subdirectories !!!


//------------------------------------------------------------------------------------------------

Sub FileSearchByHavrda_Example_of_procedure_calling()
'
' Example of FileSearchByHavrda procedure calling as replacement of missing FileSearch function in the newest MS Office VBA
' 01.06.2009, Author: P. Havrda, Czech Republic
'

Dim FileNameWithPath As Variant
Dim ListOfFilenamesWithParh As New Collection    ' create a collection of filenames

' Filling a collection of filenames (search Excel files including subdirectories)
Call FileSearchByHavrda(ListOfFilenamesWithParh, "C:\Temp", "*.xls", True)

' Print list to immediate debug window and as a message window
For Each FileNameWithPath In ListOfFilenamesWithParh    ' cycle for list(collection) processing
        Debug.Print FileNameWithPath & Chr(13)
        MsgBox FileNameWithPath & Chr(13)
Next FileNameWithPath

' Print to immediate debug window and message if no file was found
If ListOfFilenamesWithParh.Count = 0 Then
    Debug.Print "No file was found !"
    MsgBox "No file was found !"
End If

End Sub

//------------------------------------------------------------------------------------------------

Private Sub FileSearchByHavrda(pFoundFiles As Collection, pPath As String, pMask As String, pIncludeSubdirectories As Boolean)
'
' Search files in Path and create FoundFiles list(collection) of file names(path included) accordant with Mask (search in subdirectories if enabled)
' 01.06.2009, Author: P. Havrda, Czech Republic
'

Dim DirFile As String
Dim CollectionItem As Variant
Dim SubDirCollection As New Collection

' Add backslash at the end of path if not present
pPath = Trim(pPath)
If Right(pPath, 1) <> "\" Then pPath = pPath & "\"

' Searching files accordant with mask
DirFile = Dir(pPath & pMask)
Do While DirFile <> ""
pFoundFiles.Add pPath & DirFile  'add file name to list(collection)
DirFile = Dir ' next file
Loop

' Procedure exiting if searching in subdirectories isn't enabled
If Not pIncludeSubdirectories Then Exit Sub

' Searching for subdirectories in path
DirFile = Dir(pPath & "*", vbDirectory)
Do While DirFile <> ""
    ' Add subdirectory to local list(collection) of subdirectories in path
    If DirFile <> "." And DirFile <> ".." Then If ((GetAttr(pPath & DirFile) And vbDirectory) = 16) Then SubDirCollection.Add pPath & DirFile
    DirFile = Dir 'next file
Loop

' Subdirectories list(collection) processing
For Each CollectionItem In SubDirCollection
     Call FileSearchByHavrda(pFoundFiles, CStr(CollectionItem), pMask, pIncludeSubdirectories) ' Recursive procedure call
Next

End Sub

//------------------------------------------------------------------------------------------------

TA的精华主题

TA的得分主题

发表于 2010-2-17 21:27 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
正为这个烦恼呢,已下载附件,测试一下!

TA的精华主题

TA的得分主题

发表于 2010-4-26 23:06 | 显示全部楼层
顶一下,三戒老师你的课讲的很生动,受教了,我看了一下你的论坛人气很旺呀,恭喜呀!
多发表一些精典内容哦。祝你越来越旺,加油!

TA的精华主题

TA的得分主题

发表于 2010-5-4 14:14 | 显示全部楼层
在2003中使用:我的为什么提示“程序目录下没有FileSearch.dll文件,无法使用该功能”?
在2007中打开文件一点反应也没有

[ 本帖最后由 steven064 于 2010-5-4 14:36 编辑 ]

TA的精华主题

TA的得分主题

发表于 2010-5-21 16:21 | 显示全部楼层
老师,对于这个插件我还没有进行尝试,因为现在正处于初级阶段,所以先收藏等随便再使用。但现在有一个小问题想问一下:
培训中在批量打印文件时,我把你的代码进行了再编写,但一直出现错误(我已认真检查,代码好象应该没错呀!): 三戒老师.jpg 提示,为什么会出现保存提示呢?savechanges:=False 不是就是不进行保存的设置吗?请指导!谢谢!!

TA的精华主题

TA的得分主题

发表于 2010-5-22 11:50 | 显示全部楼层
三戒老师好,听了您的课程,感觉您非常认真的在讲授。来到这里看到这些作品,虽然看不懂,可踏实谨慎的精神太值得我学习了。

TA的精华主题

TA的得分主题

发表于 2010-6-20 16:37 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
支持老师~~

TA的精华主题

TA的得分主题

发表于 2010-6-25 19:32 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
收藏、好好学习学习
谢谢楼主

TA的精华主题

TA的得分主题

发表于 2010-7-26 14:00 | 显示全部楼层
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-3-29 01:15 , Processed in 0.048296 second(s), 9 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表