VBA7种文档遍历法
Sub 在选定文档最后加入一句话() '遍历文件
Dim MyDialog As FileDialog
On Error Resume Next
Application.ScreenUpdating = False
Set MyDialog = Application.FileDialog(msoFileDialogFilePicker)
With MyDialog
' .InitialFileName = "C:\"
.Filters.Clear '清除所有文件筛选器中的项目
.Filters.Add "所有 WORD 文件", "*.doc", 1 '增加筛选器的项目为所有WORD文件
.AllowMultiSelect = True '允许多项选择
If .Show = -1 Then '确定
For Each i In .SelectedItems '在所有选取项目中循环
With Documents.Open(i, , , , , , , , , , , False)
.Range.InsertAfter Chr$(13) & "改成你想加入的话................"
.Close True
End With
Next
End If
End With
Application.ScreenUpdating = True
End Sub
Sub 简单遍历测试()
For Each F In Dir遍历 'Office2003遍历,FSO遍历,双字典遍历,CMD遍历,栈遍历,管道遍历,Dir遍历
'此处加入文件处理代码即可。
Selection.InsertAfter F & Chr()
i = i +
Next
Selection.InsertAfter i
MsgBox "OKOK!!!", vbOKOnly, "OKKO"
End Sub Sub 单个文档处理(F)
Dim pa As Paragraph, c As Range
With Documents.Open(F, Visible:=False)
For Each pa In .Paragraphs
For Each c In pa.Range.Characters
If c.Font.Name = "仿宋" And Abs(Asc(c)) > Then
c.Font.Name = "仿宋_GB2312"
ElseIf c.Font.Name = "仿宋" And Abs(Asc(c)) < Then
c.Font.Name = "Times New Roman"
End If
Next
Next
.Close True
End With
End Sub ' 遍历文件夹
Function CMD遍历()
Dim arr
Dim t: t = Timer
With Application.FileDialog(msoFileDialogFolderPicker)
' .InitialFileName = "D:\" '若不加这句则打开上次的位置
If .Show <> - Then Exit Function
fod = .InitialFileName
End With
CMD遍历文件 arr, fod, "*.doc*"
arr = Filter(arr, "*", False, vbTextCompare)
CMD遍历 = arr
End Function Function 栈遍历()
Dim arr() As String
Dim t: t = Timer
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show <> - Then Exit Function
fod = .InitialFileName
End With
遍历栈 arr, CStr(fod), "doc*", True '这种方式就不用使用Function在函数中返回了
栈遍历 = arr
End Function Function 管道遍历()
Dim t: t = Timer
Dim a As New DosCMD
Dim arr
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show <> - Then Exit Function
fod = .InitialFileName
End With
a.DosInput Environ$("comspec") & " /c dir " & Chr() & fod & "\*.doc*" & Chr() & " /s /b /a:-d"
arr = a.DosOutPutEx '默认等待时间120s
arr = Split(arr, vbCrLf) '分割成数组
arr = Filter(arr, ".doc", True, vbTextCompare) '仅保留doc文件
arr = Filter(arr, "*", False, vbTextCompare)
arr = Filter(arr, "$", False, vbTextCompare)
管道遍历 = arr
'For Each F In arr
' If InStr(F, "$") = 0 And F <> "" Then
' Debug.Print F
' '单个文档处理代码 (F)'------------------------------------------------------------------------------★★★★★★★★★★★★★★★
' End If
'Next
'MsgBox "已完成!!!", vbOKCancel, "代码处理"
End Function Function AllName() '遍历获得文件名,交给数组,不变的部分;'选定的所有word文档
With Application.FileDialog(msoFileDialogFilePicker)
.Filters.Add "选择03版word文档", "*.doc",
.Filters.Add "所有文件", "*.*",
If .Show <> - Then Exit Function
For Each F In .SelectedItems
If InStr(F, "$") = Then
str0 = str0 & F & Chr()
End If
Next
End With
AllName = Left(str0, Len(str0) - )
End Function Function AllFodName() '用dos命令遍历选定文件夹下的所有word文档
Dim fso As Object
Dim aCollection As New Collection
Set fso = CreateObject("scripting.filesystemobject")
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "选择文档所在文件夹"
If .Show <> - Then Exit Function
folder = .SelectedItems()
End With
Set ws = CreateObject("WScript.Shell")
' ws.Run Environ$("comspec") & " /c dir " & folder & "\*.ppt /s /a:-d /b/on|find /v" & Chr(34) & ".pptx" & Chr(34) & "> C:\temp.txt", 0, True
ws.Run Environ$("comspec") & " /c dir " & Chr() & folder & Chr() & "\*.doc* /s /a:-d /b/on" & "> C:\temp.txt", , True Open "C:\temp.txt" For Input As #
arr = Split(StrConv(InputB(LOF(), ), vbUnicode), vbCrLf)
Close #
ws.Run Environ$("comspec") & " /c del /q /s " & Chr() & "C:\temp.txt" & Chr(), , False '删除临时文件
Set ws = Nothing
' '--------------------------此处是否多此一举?-----------------------
' For i = LBound(arr) To UBound(arr) - 1 '使用集合提高效率
' aCollection.Add arr(i)
' Next
' '--------------------------------------------------------------------
' For i = 0 To UBound(arr)
'' aname = CreateObject("Scripting.FileSystemObject").GetBaseName(arr(i))
'' If InStr(1, aname, "$") = 0 Then
' If InStr(1, arr(i), "$") = 0 Then Debug.Print arr(i)
' Selection.InsertAfter arr(i)
'' End If
' Next
AllFodName = arr
End Function Function FSO遍历() '我的得意代码之十五!!!文档不引用
'*------------------------------------------------------------------------------*
Dim fso As Object, b As Object, arr() As String, F '注意,这里的as string是必须,否则,filter函数无法使用。因为收集的不是字符串形式的地址
Set fso = CreateObject("scripting.filesystemobject")
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show <> - Then Exit Function
fod = .InitialFileName
End With
For Each F In fso.GetFolder(fod).Files '目录本身的
ReDim Preserve arr(i)
arr(i) = F
i = UBound(arr) +
Next
查找子目录 fod, arr, fso
arr = Filter(arr, ".doc", True, vbTextCompare) '仅保留doc文件
arr = Filter(arr, "*", False, vbTextCompare)
arr = Filter(arr, "$", False, vbTextCompare) '过滤掉带有$符号的文件
FSO遍历 = arr
Set fso = Nothing
End Function
Function 查找子目录(ByVal fod As String, arr, fso)
If fso.FolderExists(fod) Then
If Len(fso.GetFolder(fod)) = Then
Debug.Print "文件夹" & fod & " 是空的!" '这里似乎用不上
Else
For Each zi In fso.GetFolder(fod).SubFolders
For Each F In zi.Files '子目录中的
i = UBound(arr) +
ReDim Preserve arr(i)
arr(i) = F
Next
查找子目录 zi, arr, fso
Next
End If
End If
End Function Function Dir遍历()
Dim arr() As String
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show <> - Then Exit Function
fod = .InitialFileName
End With
处理子目录 fod, arr
arr = Filter(arr, ".doc", True, vbTextCompare) '仅保留doc文件
arr = Filter(arr, "$", False, vbTextCompare) '过滤掉带有$符号的文件
Dir遍历 = arr
End Function
Sub 处理子目录(p, arr)
On Error Resume Next
Dim a As String, b() As String, c() As String
If Right(p, ) <> "\" Then p = p + "\"
MY = Dir(p, vbDirectory Or vbHidden Or vbNormal Or vbReadOnly)
Do While MY <> ""
If MY <> ".." And MY <> "." Then
If (GetAttr(p + MY) And vbDirectory) = vbDirectory Then
n = n +
ReDim Preserve b(n)
b(n - ) = MY
Else
On Error Resume Next
i = UBound(arr) +
On Error GoTo
ReDim Preserve arr(i)
arr(i) = p + MY
End If
End If
MY = Dir
Loop
For j = To n -
处理子目录 (p + b(j)), arr
Next
ReDim b()
End Sub Function Office2003遍历() '-------------参考
Dim sFile As String, arr() As String
With Application.FileDialog(msoFileDialogFolderPicker)
' .InitialFileName = "D:\" '若不加这句则打开上次的位置
If .Show <> - Then Exit Function
bc = .InitialFileName
End With
Set mySearch = Application.FileSearch '定义一个Application.FileSearch
With mySearch
.NewSearch '设置一个新搜索
.LookIn = bc '在该驱动器盘符下
.SearchSubFolders = True '搜索子文件夹
' .FileType = msoFileTypeWordDocuments '以此可以定义文件类型
.FileName = "*.DOc*" '搜索一个指定文件,此处为任意WORD模板文件
If .Execute() > Then '开始并搜索成功
For i = To .FoundFiles.Count
ReDim Preserve arr(i - )
arr(i - ) = .FoundFiles(i)
Next i
End If
End With
Office2003遍历 = arr
End Function Function 双字典遍历() ' 字典分为word的dictionary和scripting的dictionary,这里的是后者。
Dim d1, d2 'as Dictionary
Set d1 = CreateObject("scripting.dictionary")
Set d2 = CreateObject("scripting.dictionary")
With Application.FileDialog(msoFileDialogFolderPicker)
'.InitialFileName = "D:\" '若不加这句则打开上次的位置
If .Show <> - Then Exit Function
path1 = .InitialFileName
End With
d1.Add path1, "" '目录最后一个字符必须为"\"
'*---------------------------第一个字典获取目录总数和名称----------------------------*
i = '
Do While i < d1.Count '第几个i就是进入第几个文件夹!i从0开始。d1.count为找到的文件夹总数。
ke = d1.keys
ML = Dir(ke(i), vbDirectory)
Do While ML <> ""
'Debug.Print d1.Count
If ML <> "." And ML <> ".." Then
If (GetAttr(ke(i) & ML) And vbDirectory) = vbDirectory Then '第一个括号必须有
d1.Add ke(i) & ML & "\", ""
End If
End If
ML = Dir()
Loop
i = i +
Loop
'*---------------------------第二个字典获取各个目录的文件名----------------------------*
For Each ke In d1.keys
fa = Dir(ke & "*.doc*") '也可以是“*.*”,也可以用fso操作这里
Do While fa <> ""
' d2.Add fa, "ite" 'dictionary的item可以相同,可以为空,而key决不可相同,是唯一的!
d2.Add ke & fa, "ite" 'dictionary的item可以相同,可以为空,而key决不可相同,是唯一的!【加了ke & ,完整路径;】
fa = Dir '上面的"ite"可以改成"",或任意其他值。
Loop
Next
'*--------------------------ke在这里可循环利用,打印看看key和item都是什么----------------------------*
' For Each ke In d2.keys
' Debug.Print ke
' Next
' For Each ke In d2.Items
' Debug.Print ke
' Next
'*---------------------------最后释放字典对象----------------------------*
双字典遍历 = d2.keys
Set d1 = Nothing
Set d2 = Nothing
End Function Function CMD遍历文件(ByRef arr, ByVal aPath$, ByVal aExtensionName$)
Dim aNum%
Dim t: t = Timer
With CreateObject("WScript.Shell")
If Right(aPath, ) <> "\" Then aPath = aPath & "\"
.Run Environ$("comspec") & " /c dir " & Chr() & aPath & aExtensionName & Chr() & " /s /b /a:-d > C:\tmpDoc.txt", , True '遍历获取Word文件,并列表到临时文件,同步方式
aNum = FreeFile() '空闲文件号[上面最后一个参数true的作用是等待cmd语句执行完毕后再执行下面的语句]
Open "C:\tmpDoc.txt" For Input As #aNum
arr = Split(StrConv(InputB(LOF(aNum), aNum), vbUnicode), vbCrLf) '将遍历结果从文件读取到数组中
Close #aNum
'.Run Environ$("comspec") & " /c del /q /s " & Chr(34) & "C:\tmpDoc.txt" & Chr(34), 0, False '删除临时文件,异步方式
End With
arr = Filter(arr, "$", False, vbTextCompare) '不包含$,即非word临时文件
End Function 'http://club.excelhome.net/thread-1319867-4-1.html
'原创:wzsy2_mrf Function FolderSearch(ByRef mlNameArr() As String, pPath As String, pSub As Boolean) '搜索子目录
'mlNameArr装文件名动态数组,pSub子目录开关,pPath搜索起始路径
On Error Resume Next
Dim DirFile, mf&, pPath1$
Dim workStack$(), top& 'workstack工作栈,top栈顶变量
pPath = Trim(pPath)
If Right(pPath, ) <> "\" Then pPath = pPath & "\" ' 对搜索路径加 backslash(反斜线)
pPath1 = pPath
top =
ReDim Preserve workStack( To top)
Do While top >=
DirFile = Dir(pPath1, vbDirectory)
Do While DirFile <> ""
If DirFile <> "." And DirFile <> ".." Then
If (GetAttr(pPath1 & DirFile) And vbDirectory) = vbDirectory Then
mf = mf +
ReDim Preserve mlNameArr( To mf)
mlNameArr(mf) = pPath1 & DirFile
End If
End If
DirFile = Dir
Loop
If pSub = False Then Exit Function
DirFile = Dir(pPath1, vbDirectory) ' 搜索子目录
Do While DirFile <> ""
If DirFile <> "." And DirFile <> ".." Then
If (GetAttr(pPath1 & DirFile) And vbDirectory) = vbDirectory Then
workStack(top) = pPath1 & DirFile & "\" '压栈
top = top +
If top > UBound(workStack) Then ReDim Preserve workStack( To top)
End If
End If
DirFile = Dir
Loop
If top > Then pPath1 = workStack(top - ): top = top - '弹栈
Loop
End Function Function 遍历栈(ByRef fileNameArr() As String, pPath As String, pMask As String, pSub As Boolean)
'fileNameArr装文件名动态数组,psb子目录开关,pPath搜索起始路径,pMask扩展名(如doc)
On Error Resume Next
Dim DirFile, mf&, pPath1$
Dim workStack$(), top& 'workstack工作栈,top栈顶变量
pPath = Trim(pPath)
If Right(pPath, ) <> "\" Then pPath = pPath & "\" ' 对搜索路径加 backslash(反斜线)
pPath1 = pPath
top =
ReDim Preserve workStack( To top)
Do While top >=
DirFile = Dir(pPath1 & "*." & pMask)
Do While DirFile <> ""
mf = mf +
ReDim Preserve fileNameArr( To mf)
fileNameArr(mf) = pPath1 & DirFile
DirFile = Dir
Loop
If pSub = False Then Exit Function
DirFile = Dir(pPath1, vbDirectory) ' 搜索子目录
Do While DirFile <> ""
If DirFile <> "." And DirFile <> ".." Then
If (GetAttr(pPath1 & DirFile) And vbDirectory) = vbDirectory Then
workStack(top) = pPath1 & DirFile & "\" '压栈
top = top +
If top > UBound(workStack) Then ReDim Preserve workStack( To top)
End If
End If
DirFile = Dir 'next file
Loop
If top > Then pPath1 = workStack(top - ): top = top - '弹栈
Loop
End Function
</pre>
Function fso遍历2()
Dim fso As Object, fod As Object, arr()
Set fso = CreateObject("Scripting.FileSystemObject")
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show <> - Then Exit Function
Set fod = fso.GetFolder(.SelectedItems())
End With
Call 递归(fod, arr, i)
ReDim Preserve arr(i - )
fso遍历2 = arr
Set fso = Nothing
Set fod = Nothing
End Function
Function 递归(fod, arr, i)
Dim SubFolder As Object
Dim File As Object
For Each File In fod.Files
ReDim Preserve arr(i)
arr(i) = File.Path
i = i +
Next
ReDim Preserve arr(i)
For Each SubFolder In fod.SubFolders
递归 SubFolder, arr, i
Next
End Function
Function DIR词典遍历()
Dim d1 As Object, arr()
Set d1 = CreateObject("scripting.dictionary")
With Application.FileDialog(msoFileDialogFolderPicker)
If .Show = - Then fod = .InitialFileName Else Exit Function
End With
d1.Add fod, ""
js = '词典计数器,起到类似递归的作用,随着不断的增加,逐渐深入到新加入的目录中;
Do While js < d1.Count '第几个i就是进入第几个文件夹!i从0开始。d1.count为找到的文件夹总数。
ke = d1.keys
ML = Dir(ke(js), vbDirectory)
Do While ML <> ""
If ML <> "." And ML <> ".." Then '这两个点,一个代表本目录,另一个代表上级目录parent,dir方式总会有
If (GetAttr(ke(js) & ML) And vbDirectory) = vbDirectory Then '第一个括号必须有
d1.Add ke(js) & ML & "\", ""
Else
If InStr(ML, "doc") > And InStr(ML, "$") = Then
ReDim Preserve arr(i)
arr(i) = ke(js) & ML
i = i +
End If
End If
End If
ML = Dir()
Loop
js = js +
Loop
End Function
VBA7种文档遍历法的更多相关文章
- lucene全文搜索之四:创建索引搜索器、6种文档搜索器实现以及搜索结果分析(结合IKAnalyzer分词器的搜索器)基于lucene5.5.3
前言: 前面几章已经很详细的讲解了如何创建索引器对索引进行增删查(没有更新操作).如何管理索引目录以及如何使用分词器,上一章讲解了如何生成索引字段和创建索引文档,并把创建的索引文档保存到索引目录,到这 ...
- simhash-- 一种文档去重的算法
最早看数学之美的时候,书中就提到了这个算法,当时没有做过相关地工作,没什么具体的印象.一年前转岗时面试时别人提到了这个算法,知道了simhash可以用来解决网页等海量数据的去重问题,很高效. 然后自己 ...
- 【C#附源码】数据库文档生成工具支持(Excel+Htm)
数据库文档生成工具是用C#开发的基于NPOI组件的小工具.软件源码大小不到10MB.支持生成Excel 和Html 两种文档形式.了解更多,请访问:http://www.oschina.net/cod ...
- word文档转pdf,支持.doc和.docx,另附抽取pdf指定页数的方法
公司有个需求,需要将word转成pdf并且抽取首页用以展示,word文档有需要兼容.doc和.docx两种文档格式.其中.docx通过poi直接就可以将word转成pdf,.doc则无法这样实现,上网 ...
- 使用多文档接口(Multiple Document Interface) 一
原文地址msdn:https://msdn.microsoft.com/en-us/library/windows/desktop/ms644909(v=vs.85).aspx#creating_fr ...
- 基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览
在博客园很多文章里面,曾经有一些介绍Office文档预览查看操作的,有些通过转为PDF进行查看,有些通过把它转换为Flash进行查看,但是过程都是曲线救国,真正能够简洁方便的实现Office文档的预览 ...
- web文档在线阅览
之前遇到很多各种文档在线阅览的需求,也有不少朋友经常问我这种需求的实现方案,大致试了一下网上的一些比较主流的推荐方案,但都不尽如人意,这里有一个比较全面的总结,需要的朋友可以根据自己的需求到这里查看, ...
- MongoDB文档、集合、数据库简介
文档 概述 文档是MongoDB的核心概念,是数据的基本单元,非常类似于关系数据库中的行.在MongoDB中,文档表示为键值对的一个有序集.MongoDB使用Javascript shell,文档的表 ...
- [转载]基于MVC4+EasyUI的Web开发框架经验总结(8)--实现Office文档的预览
在博客园很多文章里面,曾经有一些介绍Office文档预览查看操作的,有些通过转为PDF进行查看,有些通过把它转换为Flash进行查看,但是过程都是曲线救国,真正能够简洁方便的实现Office文档的预览 ...
随机推荐
- vs+qt使用资源文件
1.在Resources目录新建一个.qrc文件 2.在解决方案的Resource Files中添加这个文件 3.为这个qrc添加资源,建议把资源都放进Resources
- git中Untracked files如何清除
$ git status # On branch test # Untracked files: # (use "git add <file>..." to inclu ...
- 使用 if 语句
与很多编程语言一样,if 表达式用来处理逻辑条件.在 R 中,逻辑条件通常表达为某个表达式返回的单值逻辑向量.例如,我们可以写一个简单的函数 check_positive,如果输入一个正数则返回 1, ...
- Qt5_pro_02
1.g++ 编译参数 如果 用g++编译时,命令行是这样的:“g++ main.cpp -std=c++0x -pthread” 则在Qt的pro文件中这样设置: QMAKE_CXXFLAGS += ...
- Java回顾之ORM框架
这篇文章里,我们主要讨论ORM框架,以及在使用上和JDBC的区别. 概述 ORM框架不是一个新话题,它已经流传了很多年.它的优点在于提供了概念性的.易于理解的数据模型,将数据库中的表和内存中的对象建立 ...
- BooStrap4文档摘录 Utilities
border:可以用原生css实现效果.❌没看 clearfix, float, ✅ close icon ✅ colors ✅ display✅ 各种显示的格式. embed ✅ <ifr ...
- BooStrap4文档摘录: 1. Layout
文档: https://getbootstrap.com/docs/4.1/layout/overview/ w3c的案例:很直观: https://www.w3schools.com/bootst ...
- PowerDesigner16工具学习笔记-创建RQM
1.点击标准工具条中的
- UVA-1572 Self-Assembly (图+拓扑排序)
题目大意:每条边上都有标号的正方形,两个正方形能通过相匹配的边连接起来,每种正方形都有无限多个.问能否无限延展下去. 题目分析:将边视为点,正方形视为边,建立无向图,利用拓扑排序判断是图否为DAG. ...
- POJ 1426 Find the Multiple 思路,线性同余,搜索 难度:2
http://poj.org/problem?id=1426 测试了一番,从1-200的所有值都有long long下的解,所以可以直接用long long 存储 从1出发,每次向10*s和10*s+ ...