|
本帖最后由 413191246se 于 2020-8-9 21:25 编辑
* 各位朋友:下面 5 个主程序分别是 cuanju 老师、gbgbxgb 老师、duquancai 老师、小花鹿 老师 和一位挪威人 Havrda 所编写的宏代码。
* 还有两个小程序,分别是我编写的单个文档示例代码《adoc》宏和我编写的《SelectFolder》函数。
* 如果想批量排版 Word 文档(*.docx/*.doc),可以在《adoc》宏里修改代码,不要修改主程序,以免出错。
* 以下 5 个宏均在 Word2019 (根下10层子文件夹) 下测试通过,正确无误!(Word2007-2019 应该都可以。)
*(不可在 Word2003 下使用,因为过去我试过,无法运行。Word2003 可以使用挪威人所写的代码,似乎可以。)
- Sub 循环遍历文件夹及子文件夹_DIR_cuanju()
- Dim strFileFilter As String
- Dim strFileName As String, strType As String
- Dim StartFolder As String
- Dim FolderList As Object, FileList As Object
- Dim FolderName, arr1
- Dim oD As Document, i&, x&, m&, n&
- strFileFilter = "doc*"
- Set FolderList = CreateObject("Scripting.Dictionary")
- Set FileList = CreateObject("Scripting.Dictionary")
- StartFolder = SelectFolder
- FolderList.Add StartFolder, ""
- Do While FolderList.Count > 0
- For Each FolderName In FolderList.keys
- strFileName = Dir(FolderName, vbDirectory)
- Do While strFileName <> ""
- If strFileName <> ".." And strFileName <> "." Then
- If GetAttr(FolderName & strFileName) And vbDirectory Then
- FolderList.Add FolderName & strFileName & "", ""
- m = m + 1
- Else
- i = InStrRev(strFileName, ".")
- strType = Right(strFileName, Len(strFileName) - i)
- If strType Like strFileFilter Then
- FileList.Add FolderName & strFileName, ""
- End If
- n = n + 1
- End If
- End If
- strFileName = Dir
- Loop
- FolderList.Remove (FolderName)
- Next
- Loop
- For Each arr1 In FileList.keys
- Set oD = Documents.Open(arr1)
- adoc
- oD.Close True
- x = x + 1
- Next
- Set FolderList = Nothing
- Set FileList = Nothing
- MsgBox "文件夹包含 " & n & " 个文件!" & m & " 个子文件夹!" & vbCr & "共处理 Word 文档(*.docx/*.doc) " & x & " 个!", 0 + 48
- End Sub
- Sub 循环遍历文件夹及子文件夹_DIR_gbgbxgb()
- Dim d As Object, thePath$, theStr$, i&, j&, k&, doc As Document
- thePath = SelectFolder
- Set d = CreateObject("Scripting.Dictionary")
- d(thePath) = ""
- Do While i < d.Count
- thePath = d.keys()(i)
- theStr = Dir(thePath, vbDirectory)
- Do While theStr <> ""
- If theStr <> "." And theStr <> ".." Then
- If (GetAttr(thePath & theStr) And vbDirectory) = vbDirectory Then
- d(thePath & theStr & "") = ""
- Else
- j = j + 1
- If thePath & theStr Like "*.doc*" Then
- Set doc = Documents.Open(FileName:=thePath & theStr)
- adoc
- doc.Close savechanges:=wdSaveChanges
- k = k + 1
- End If
- End If
- End If
- theStr = Dir
- Loop
- i = i + 1
- Loop
- Set d = Nothing
- MsgBox "文件夹包含 " & j & " 个文件!" & i - 1 & " 个子文件夹!" & vbCr & "共处理 Word 文档(*.docx/*.doc) " & k & " 个!", 0 + 48
- End Sub
- Sub 循环遍历文件夹及子文件夹_DIR_xiaohualu()
- Dim d, n&, m&, x&, mydir, dk, doc As Document, i&
- Set d = CreateObject("Scripting.Dictionary")
- d(SelectFolder) = ""
- Do While n < d.Count
- dk = d.keys
- mydir = Dir(dk(n), vbDirectory)
- Do While mydir <> ""
- If mydir <> "." And mydir <> ".." Then
- If GetAttr(dk(n) & mydir) = vbDirectory Then
- d(dk(n) & mydir & "") = ""
- m = m + 1
- Else
- x = x + 1
- If dk(n) & mydir Like "*.doc*" Then
- Set doc = Documents.Open(FileName:=dk(n) & mydir)
- adoc
- doc.Close savechanges:=wdSaveChanges
- i = i + 1
- End If
- End If
- End If
- mydir = Dir
- Loop
- n = n + 1
- Loop
- Set d = Nothing
- Set dk = Nothing
- MsgBox "文件夹包含 " & x & " 个文件!" & m & " 个子文件夹!" & vbCr & "共处理 Word 文档(*.docx/*.doc) " & i & " 个!", 0 + 48
- End Sub
- Sub 循环遍历文件夹及子文件夹_FSO_duquancai()
- Dim pPath$, f As Object, fd As Object, fso As Object, Stack$(), top&, n&, stxt$, doc As Document, x&
- pPath = SelectFolder
- Set fso = CreateObject("Scripting.FileSystemObject")
- top = 1
- ReDim Stack(0 To top)
- Do While top >= 1
- For Each f In fso.getfolder(pPath).Files
- n = n + 1
- stxt = f.Path
- If stxt Like "*.doc*" Then
- Set doc = Documents.Open(FileName:=stxt)
- adoc
- doc.Close savechanges:=wdSaveChanges
- x = x + 1
- End If
- Next
- For Each fd In fso.getfolder(pPath).SubFolders
- Stack(top) = fd.Path
- top = top + 1
- If top > UBound(Stack) Then ReDim Preserve Stack(0 To top)
- Next
- If top > 0 Then pPath = Stack(top - 1): top = top - 1
- Loop
- Set f = Nothing
- Set fd = Nothing
- Set fso = Nothing
- MsgBox "文件夹包含 " & n & " 个文件!" & vbCr & "共处理 Word 文档(*.docx/*.doc) " & x & " 个!", 0 + 48
- End Sub
- Sub adoc()
- With ActiveDocument.Paragraphs(1).Range
- .InsertBefore Text:="Loop Folders!" & vbCr
- .Bold = True
- .Underline = wdUnderlineSingle
- .Font.ColorIndex = wdRed
- .ParagraphFormat.Alignment = wdAlignParagraphJustify
- End With
- End Sub
- Function SelectFolder() As String
- With Application.FileDialog(msoFileDialogFolderPicker)
- If .Show Then SelectFolder = .SelectedItems(1) & "" Else End
- End With
- If MsgBox("是否选择文件夹 " & """" & SelectFolder & """" & " ?", 4 + 16) = vbNo Then End
- End Function
- Sub 循环遍历文件夹及子文件夹_DIR_Havrda()
- Dim n&, doc As Document
- Dim FileNameWithPath As Variant, ListOfFilenamesWithParh As New Collection
- Call FileSearchByHavrda(ListOfFilenamesWithParh, SelectFolder, "*.doc*", True)
- For Each FileNameWithPath In ListOfFilenamesWithParh
- Set doc = Documents.Open(FileName:=FileNameWithPath)
- adoc
- doc.Close savechanges:=wdSaveChanges
- n = n + 1
- Next FileNameWithPath
- If ListOfFilenamesWithParh.Count = 0 Then MsgBox "File not found!"
- MsgBox "处理完毕!共处理 Word 文档(*.docx/*.doc) " & n & " 个!", 0 + 48
- End Sub
- Sub FileSearchByHavrda(pFoundFiles As Collection, pPath As String, pMask As String, pIncludeSubdirectories As Boolean)
- Dim DirFile As String, CollectionItem As Variant, SubDirCollection As New Collection
- pPath = Trim(pPath)
- If Right(pPath, 1) <> "" Then pPath = pPath & ""
- DirFile = Dir(pPath & pMask)
- Do While DirFile <> ""
- pFoundFiles.Add pPath & DirFile
- DirFile = Dir
- Loop
- If Not pIncludeSubdirectories Then Exit Sub
- DirFile = Dir(pPath & "*", vbDirectory)
- Do While DirFile <> ""
- If DirFile <> "." And DirFile <> ".." Then
- If ((GetAttr(pPath & DirFile) And vbDirectory) = 16) Then SubDirCollection.Add pPath & DirFile
- End If
- DirFile = Dir
- Loop
- For Each CollectionItem In SubDirCollection
- Call FileSearchByHavrda(pFoundFiles, CStr(CollectionItem), pMask, pIncludeSubdirectories)
- Next
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|