|
楼主,如果是 Word2003,可以用 FileSearch 对象遍历文件夹及子文件夹;如果是 2007 及以上,须用 dir 遍历。下面是 Word2003 遍历文件夹及子文件夹示例代码:
- Sub 批量打印()
- On Error Resume Next
- Dim fd As FileDialog, i&, doc As Document, p$, s As Section, j&, k$, x&, t&
- Set fd = Application.FileDialog(msoFileDialogFolderPicker)
- If fd.Show = -1 Then p = fd.SelectedItems(1) Else Exit Sub
- Set fd = Nothing
- If MsgBox("是否打印文件夹 " & p & " ?", 4 + 48) = vbNo Then Exit Sub
- k = InputBox("请输入打印份数!", "批量打印", "1")
- If k = "" Then Exit Sub
- For t = 1 To k
- With Application.FileSearch
- .NewSearch
- .LookIn = p
- .SearchSubFolders = True
- .FileName = "*.doc"
- If .Execute > 0 Then
- For i = 1 To .FoundFiles.Count
- Set doc = Documents.Open(FileName:=.FoundFiles(i), Visible:=False)
- For Each s In doc.Sections
- With s.PageSetup
- If .Orientation = wdOrientLandscape Then j = 1 Else j = 0
- If .PaperSize <> wdPaperA4 Then .PaperSize = wdPaperA4: If j = 1 Then .Orientation = wdOrientLandscape
- End With
- Next
- doc.PrintOut
- doc.Close savechanges:=wdDoNotSaveChanges
- Next i
- x = .FoundFiles.Count
- Else
- MsgBox "未发现文件!", 0 + 16: End
- End If
- End With
- Dialogs(wdDialogNewToolbar).Display timeout:=1 '延时
- Next t
- MsgBox "打印完毕!共打印 " & x & " 个文件!" & k & " 份!", 0 + 48
- End Sub
复制代码 |
|