用VBS写了一个批量打印word文档,功能是批量打印文件夹内所有的word文档,并能选择打印的页码范围。代码中使用printout函数,它的参数起不了作用。代码如下:- nput_pages = InputBox("要打印的页码和页码范围。例如:2, 6-10。表示打印第 2 页以及第 6 至第 10 页","word批量打印","整个文档")
- input_copies = InputBox("每个文档要打印的份数:","word批量打印","1")
-
- APP = "Word"
-
- Print_Dir App, SelectFolder
-
- Sub Print_Dir(APP,FolderPath)
- '本过程用于遍历目录下的文件,并打印
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set AppObj = CreateObject(APP & ".Application")
- xxx1 = "doc" : xxx2 = "docx"
- AppObj.Visible = 0 :coun = 0
-
- If fso.FolderExists(FolderPath) Then
- Set Folder = fso.GetFolder(FolderPath)
- Set Files = Folder.Files
- fileNums = Files.Count
- time1 = Time
-
- For Each File In Folder.Files
-
- If right(File.name,3)= xxx1 Or right(File.name,4)= xxx2 then
- AppObj.Documents.Open(FolderPath & "\"& File.Name)
- AppObj.Windows(1).Activate
- If input_pages = "整个文档" then
- AppObj.PrintOut ,,,,,,,input_copies
- AppObj.Documents(1).Close False
- coun = coun +1
- Else
- AppObj.PrintOut ,,,,,,,input_copies,input_pages
- AppObj.Documents.Close False
- coun = coun +1
- End if
- End if
- Next
- time2 = Time
- MsgBox "共有文件" & fileNums & "个,打印文件" & coun & "个" & vbCrLf & "用时: " & DateDiff("s", time1, time2) & "秒"
- End if
- AppObj.Quit: Set AppObj = Nothing :Set fso = Nothing
- End Sub
-
- Function SelectFolder()
- Const WINDOW_HANDLE = 0
- Const OPTIONS = &H10
- Set objShell = CreateObject("Shell.Application")
- Set objFolder = objShell.BrowseForFolder _
- (WINDOW_HANDLE, "请选择你要打印的文件夹:", OPTIONS , "0")
-
- If objFolder Is Nothing Then
- Wscript.Quit
- End if
- Set objFolderItem = objFolder.Self
- SelectFolder = objFolderItem.Path
- Set objShell = nothing
- End Function
复制代码 运行后,输入任何页码都是打印整个文档。求大神们不吝赐教。 |