本帖最后由 yu2n 于 2017-5-22 22:10 编辑
回复 4# ygqiang
VBS 列印 WORD DOC 檔,想要多慢就多慢,急死等單人專用。 By Yu2n
可指定列印範圍,可指定每頁等待時間。(Ps: 这个需求真的很搞笑)
- Option Explicit
-
- '************************************************************************
- 'VBS 列印 WORD DOC 檔,想要多慢就多慢,急死等單人專用。 By Yu2n
- '可指定列印範圍,可指定每頁等待時間。(Ps: 这个需求真的很搞笑)
- '************************************************************************
- Main
- Sub Main()
-
- Const wdPrintRangeOfPages = 4
- Const wdPrintDocumentContent = 0
- Const wdNumberOfPagesInDocument = 4
-
- Dim strDoc, lngSecond
- strDoc = "C:\Test.doc" '要列印的 DOC 檔路徑
- lngSecond = 5 '每頁列印延時--5秒
-
- '******************************
- '打開 DOC,獲取 Word 對象
- '******************************
- On Error Resume Next
- Dim oWord, oDoc, nPages, sPages, sPages2, vT, nT
- Set oWord = GetObject(, "Word.Application")
- If Err Then
- Err.Clear
- Set oWord = CreateObject("Word.Application")
- If Err Then
- MsgBox "沒有安裝 Office Word !", vbQuestion, WScript.ScriptName
- Exit Sub
- End If
- End If
- Set oDoc = GetObject(strDoc)
- If Err Then
- MsgBox """" & strDoc & """ 不存在,或者不是 Office Word 文件!", vbQuestion, WScript.ScriptName
- oDoc.Close False
- oWord.Quit
- Exit Sub
- End If
- On Error GoTo 0
- oDoc.Activate
- Set oWord = oDoc.Application
- oWord.Application.DisplayAlerts = False
- oWord.Visible = True
-
- '******************************
- '獲取用戶輸入頁數,格式化(2, 6-10 => 2,6,7,8,9,10)
- '******************************
- nPages = oDoc.Application.Selection.Information(wdNumberOfPagesInDocument)
- sPages = InputBox("請輸入要列印的頁碼和頁面範圍,以逗號隔開。例如,""2, 6-10"" 會就列印第 2 頁及第 6 至第 10 頁。","請輸入要列印的頁碼範圍:","1-" & nPages)
- For Each vT In Split(sPages, ",")
- If InStr(CStr(vT), "-") <= 0 Then
- If sPages2 <> "" Then sPages2 = sPages2 & ","
- sPages2 = sPages2 & CStr(vT)
- Else
- For nT = Split(CStr(vT),"-")(0) To Split(CStr(vT),"-")(1)
- If sPages2 <> "" Then sPages2 = sPages2 & ","
- sPages2 = sPages2 & CStr(nT)
- Next
- End If
- Next
-
- '******************************
- '打印頁面(每次列印1頁後延時列印下一頁)
- '******************************
- For nT = 0 To UBound(Split(sPages2, ","))
- oDoc.Application.PrintOut True, True, wdPrintRangeOfPages, "", "", "", wdPrintDocumentContent, 1, CStr(Split(sPages2, ",")(nT)), 0
- If nT < UBound(Split(sPages2, ",")) Then WScript.Sleep lngSecond * 1000
- Next
-
- '******************************
- '關閉 Word
- '******************************
- oDoc.Close False
- oWord.Quit
- MsgBox "列印完成 !", vbQuestion, WScript.ScriptName
-
- End Sub
复制代码
|