打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
VBA实例:Word文档内容搜索器,文件遍历,当前位置下子文件夹遍历(by daode12...

'''Word文档内容搜索器,文件遍历,当前位置下子文件夹遍历
'''输出于Excel/Sheet2中
'''存在的用"***"表示,不存在的用"---"表示。
'''要搜索的字符在openWord中:xStr ="???"中定义。
'''本VBA用于Excel,可粘贴于模块中,再运行宏。
'''------by daode1212 , 2010-10-20

Dim nFile As Integer

Sub 遍历子文件在DOC中搜索字符()
'''DOC搜索主程序入口:
   nFile = 0
   way = ThisWorkbook.path
   getAllFolder CStr(way)
End Sub

Sub getAllFolder(path)
'''遍历文件夹:
Set fso = CreateObject("Scripting.FileSystemObject")
Set objfolder = fso.GetFolder(path)
Set objSubFolders = objfolder.SubFolders
Set objfolder = Nothing
getAllFile path
For Each objSubFolder In objSubFolders
    nowPath = CStr(path & "\" & objSubFolder.Name)
    getAllFolder nowPath
    '''getAllFile nowPath
Next
Set fso = Nothing
End Sub

Sub getAllFile(fold)
'''遍历文件,输出路径与文件名:
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set objfiles = fso.GetFolder(fold)
    For Each objfile In objfiles.Files
        nowFile = objfile.Name
        If LCase(Right(nowFile, 4)) = ".doc" Then
          nFile = nFile + 1 '文件个数,写入Sheet2中的行标;
          Sheet2.Cells(nFile, 2) = fold
          Sheet2.Cells(nFile, 3) = nowFile
          curPF = fold & "\" & nowFile
          openWord curPF, nFile
        End If
    Next
    Set objfiles = Nothing
    Set fso = Nothing
End Sub

Sub openWord(curPF, nFile)
'''搜索字符串:
xStr = "防雪防冻"
    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = False
    Set wrdDoc = wrdApp.Documents.Open(curPF)
    strText = wrdDoc.Range.Text() '读取全文;
    If InStr(strText, xStr) Then
       Sheet2.Cells(nFile, 1) = "***" '找到的文件;
    Else
       Sheet2.Cells(nFile, 1) = "---" '未找到的文件;
    End If
    wrdDoc.Close
    wrdApp.Quit
    Set wrdDoc = Nothing
    Set wrdApp = Nothing
End Sub

'''课题之一:将本程序修改为文本的替换;
'''课题之二:将本程序修改为特有文本的剥取;

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
三、遍历目标文件夹内所有文件、以及所有子文件夹中的所有文件。
VBA遍历所有文件夹的两种方法(filesearch和FileSystemObject)
Excel VBA 遍历*.xlsx文件的方法
VBA实用小程序72:遍历文件夹(和子文件夹)中的文件
VBA 之递归遍历文件
【制作参赛卡】Word批量插入图片
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服