2016-08-25 46 views
0

我有一個VBS查看一組文件並將它們打印到PDF。我現在遇到的問題是它不打印整個工作簿。如果有一個字符串,我可以添加到下面的代碼?VBS命令告訴excel打印整個工作簿

Set fso = CreateObject("Scripting.FileSystemObject") 
currentdir = fso.GetAbsolutePathName(".") 

Set xmldom = CreateObject("MSXML.DOMDocument") 
xmldom.Load(currentdir & "\info.xml") 

progid = xmldom.SelectSingleNode("/xml/progid").text 

set obj = CreateObject(progid) 

printername = obj.GetPrinterName 

runonce = obj.GetSettingsFileName(true) 

Set fldr = fso.GetFolder(currentdir & "\in") 
cnt = 0 
For Each f In fldr.files 
cnt = cnt + 1 
output = currentdir & "\out\" & Replace(f.name, ".xls", "") & ".pdf" 

obj.Init 
obj.SetValue "Output", output 
obj.SetValue "ShowSettings", "never" 
obj.SetValue "ShowPDF", "no" 
obj.SetValue "ShowProgress", "no" 
obj.SetValue "ShowProgressFinished", "no" 
obj.SetValue "SuppressErrors", "yes" 
obj.SetValue "ConfirmOverwrite", "no" 

obj.WriteSettings True 

printfile = currentdir & "\in\" & f.name 
cmd = """" & currentdir & "\printto.exe"" """ & printfile & """ """ &  printername & """" 

Set WshShell = WScript.CreateObject("WScript.Shell") 
ret = WshShell.Run(cmd, 1, true) 

While fso.fileexists(runonce) 
    wscript.sleep 100 
Wend 
Next 

set obj = Nothing 

Wscript.Echo cnt & " documents were printed." 
+0

找到一些可能會對你有幫助的文章。 http://excel.tips.net/T002001_Printing_an_Entire_Workbook_by_Default.html http://stackoverflow.com/questions/5693189/excel-vb-script-to-print-all-workbooks-sheets @John E –

回答

0

我相信printto.exe是一個第三方工具,它可從http://www.reasoft.com/products/pdfprinter/help/using/se/command.html

您可能需要軟件供應商聯繫,以確認是否要由實用程序支持什麼。

無論如何,您可能想要遵循上面@ randy-schuman指出的建議,即從Excel應用程序中觸發打印,而不是使用第三方實用程序。你可以做下面的事情:

Set objExcel = CreateObject("Excel.Application") 
objExcel.DisplayAlerts = False 

For Each f In fldr.Files 
    If Instr(f.Type,"Excel") Then 
    Set book = objExcel.Workbooks.Open(f.Path,0,1) 
    book.PrintOut ,,,,printername '' https://msdn.microsoft.com/en-us/library/office/ff196840.aspx 
    book.Close 0 
    End If 
Next 

objExcel.Quit 
Set objExcel = Nothing