|
运行“Selected_Folder()”选择文件夹:
- Sub Selected_Folder()
- Dim FN As String, myPath$, fso As Object, i#, w$, j%
- '微软Excel VBA 默认选择文件夹的Dialog对话框
- With Application.FileDialog(msoFileDialogFolderPicker) '运行后出现标准的选择文件夹对话框
- If .Show Then myPath = .SelectedItems(1) Else Exit Sub '如选中则返回=-1 / 取消未选则返回=0
- End With
- If Right(myPath, 1) <> "" Then myPath = myPath & ""
- Call ListAllFso(myPath)
- MsgBox "操作完成!", 0 + 64
- End Sub
- Function ListAllFso(myPath$) '用FSO方法遍历并列出所有文件和文件夹名的【递归过程】
- Set fld = CreateObject("Scripting.FileSystemObject").GetFolder(myPath)
- '用FSO方法得到当前路径的文件夹对象实例 注意这里的【当前路径myPath是个递归变量】
- If Right(myPath, 1) <> "" Then myPath = myPath & ""
- FN = Dir(myPath & "*.doc*") '循环所选择文件夹目录下各个文件
- Do While FN <> "" '当文件名不为空时持续循环
- ' If InStr(FN, "*.doc*") > 0 Then
- With Documents.Open(myPath & FN)
- Call ReplaceFonts
- .Close True
- End With
- ' End If
- FN = Dir '循环到下一个文件
- Loop
- For Each fd In fld.SubFolders '遍历当前文件夹内所有【子文件夹.SubFolders】
- Call ListAllFso(fd.Path) '注意此时的路径变量已经改变为【子文件夹的路径fd.Path】
- '注意重点在这里: 继续向下调用递归过程【遍历子文件夹内所有文件文件夹对象】
- Next
- End Function
- Sub ReplaceFonts() '替换Word中下划线内容的字体
- Dim s$
- With ActiveDocument.Content
- With .Find
- .ClearFormatting
- .Text = ""
- .Font.Underline = wdUnderlineSingle '查找下划线
- .Replacement.Text = ""
- .Forward = True
- .MatchWildcards = False
- Do While .Execute
- With .Parent
- .Font.Name = "仿宋GB_2312"
- ' .Font.Underline = wdUnderlineNone '取消下划线
- .Start = .End
- End With
- Loop
- End With
- End With
- End Sub
复制代码
|
|