2017-10-16 142 views
1

我不知道如何按順序打印PDF文件(單元格列表的順序)。如何使用Excel-VBA按單元格列表的順序打印PDF文件?

Sub PrintPDFFiles() 
    zProg = "C:\Program Files\Adobe\Reader 11.0\Reader\AcroRd32.exe" 
    zLastRow = [a65536].End(xlUp).Row 
    temp = "a1:a" & zLastRow 
    zPrinter = "HP LaserJet Professional M1213nf MFP " 
    For Each cell In Range(temp) 
    zFile = cell.Value 
    If zFile Like "*.pdf" Then 
    Shell """" & zProg & """/n /h /t""" & zFile & """" 
    End If 
    Next 
    End Sub 

視覺輔助: enter image description here

研究我迄今所做的:

沒有命令行switch,以便打印文件。

更新-1 mycode的後建議:

我用object.run方法在我的代碼,我得到錯誤:

Sub PrintPDFFiles() 
    zProg = "C:\Program Files\Adobe\Reader 11.0\Reader\AcroRd32.exe" 
    zLastRow = [a65536].End(xlUp).Row 
    temp = "a1:a" & zLastRow 
    zPrinter = "HP LaserJet Professional M1213nf MFP " 
    For Each cell In Range(temp) 
    zFile = cell.Value 
    If zFile Like "*.pdf" Then 
    Dim wsh As Object 
    Set wsh = VBA.CreateObject("WScript.Shell") 
    Dim waitOnReturn As Boolean: waitOnReturn = True 
    Dim windowStyle As Integer: windowStyle = 1 
    zCommand = zProg & " /n /h /t " & Chr(34) & zFile & Chr(34) & " " & zPrinter 
    wsh.Run zCommand, windowStyle, waitOnReturn 
    End If 
    Next 
    End Sub 

錯誤:

Error

Update-2 My code Afte [R建議:

wsh.Run """Acrobat.exe"" /n /h /t" & Chr(34) & zfile & Chr(34) & " " & zPrinter, , waitOnReturn 

問題: 我設法用run方法來打印,但是我有每個文件後關閉Adobe Acrobat Reader軟件。我必須打印500多個文件。

+0

你是**殼牌**循環;你確定一個** Shell **在下一個啓動之前完成? –

+0

Adob​​e在打印完成之前保持打開狀態嗎?如果您嘗試將Adobe聲明爲對象,則打印,Adobe.Quit,然後執行「雖然不是Adobe」。我沒有太多操作Adobe的經驗,但這是一種可行的方法。 –

+0

請參閱:https://stackoverflow.com/questions/15951837/wait-for-shell-command-to-complete –

回答

0

我修改代碼使用Ghostscript

子PrintPDFFiles()

zProg = "gsprint -printer printerName -dPDFFitPage " 
zLastRow = [a65536].End(xlUp).Row 

Dim Counter As Integer 

For Counter = 1 To zLastRow 

    zFile = Worksheets("Sheet1").Cells(Counter, 1).Value ' Where 1 is the first column 
    If zFile Like "*.pdf" Then 
     Shell """" & zProg & zFile & """" 
    End If 

Next Counter 

末次

+0

我沒有看到等待shell命令在您的答案中完成。 – shaadi

+0

在http://dmcritchie.mvps.org/excel/shell.htm有一個shell樣本, – VinhCC

+0

鏈接與我的上下文無關 - 沒有waitOnReturn。 問題是我想在循環開始另一行之前完成一行。我已經在使用shell查看我的示例代碼。 – shaadi

0

使用this打印所有排序從小區列表文件,因爲文件目錄中的文件。基本上,該軟件按順序打印所有類型的文件。