|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 413191246se 于 2019-6-11 09:57 编辑
- Sub FSO批量插入文档()
- If Documents.Count = 0 Then MsgBox "没有打开的文档!", 0 + 16: End
- With Selection
- .EndKey 6
- .InsertBreak 2
- End With
- On Error Resume Next
- Dim objShell As Object, objFolder As Object, pPath$, f As Object, fd As Object, fso As Object, Stack$(), top&, n&, stxt$, doc As Document, x&
- Set objShell = CreateObject("Shell.Application")
- Set objFolder = objShell.BrowseForFolder(0, "请选择要插入的文件夹!", 0, 0)
- pPath = objFolder.self.Path & ""
- Set objShell = Nothing
- Set objFolder = Nothing
- If MsgBox("请确认!是否插入文件夹 " & pPath & " ?", 4 + 16) = vbNo Then ActiveDocument.Close 0: Exit Sub
- 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
- With Selection
- .InsertFile FileName:=stxt
- .InsertBreak Type:=wdSectionBreakNextPage
- End With
- 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
- With ActiveDocument
- .Range(Start:=.Content.End - 2, End:=.Content.End - 2).Select
- End With
- With Selection
- .Delete
- .Delete
- .HomeKey 6
- End With
- MsgBox "文件夹包含 " & n & " 个文件!" & vbCr & "共插入 Word 文档(*.docx/*.doc) " & x & " 个!", 0 + 48
- End Sub
复制代码
|
|