|
- Sub 批量转PDF()
- Application.ScreenUpdating = False
- With Application.FileDialog(msoFileDialogFolderPicker)
- .AllowMultiSelect = False
- If .Show = -1 Then Folder = .SelectedItems(1)
- End With
- If Right(Folder, 1) <> "" Then Folder = Folder & ""
- Folder = """" & Folder & """"
- With CreateObject("WScript.Shell")
- .Run Environ$("comspec") & " /c dir " & Folder & "*.doc /s /a:-d /b > C:\aTemp.txt", 0, True '调用cmd遍历结果
- aNum = FreeFile
- Open "C:\aTemp.txt" For Input As #aNum
- arr = Split(StrConv(InputB(LOF(aNum), aNum), vbUnicode), vbCrLf) '读取遍历结果
- Close #aNum
- .Run Environ$("comspec") & " /c del /f /q C:\aTemp.txt", 0, False '删除临时文件
- End With
- For i = LBound(arr) To UBound(arr) - 1 '循环
- With Documents.Open(arr(i)) '打开文件
- aName = .FullName '获取完整路径
- aName = Replace(aName, Mid(aName, InStrRev(aName, ".doc")), ".pdf") '更改扩展名为pdf
- .ExportAsFixedFormat OutputFileName:=aName, ExportFormat:=wdExportFormatPDF, OpenAfterExport:=FalseFalse, _
- OptimizeFor:=wdExportOptimizeForPrint, Range:=wdExportAllDocument, CreateBookmarks:=wdExportCreateHeadingBookmarks '另存为PDF
- .Close False '不保存关闭文档
- End With
- Next
- Application.ScreenUpdating = True
- End Sub
复制代码 |
|