VBS命令告诉excel打印整个工作簿

时间:2016-08-25 17:40:43

标签: excel vbscript

我有一个VBS,它查看一组文件并将它们打印成PDF。我现在遇到的问题是它不会打印整个工作簿。如果有一个字符串我可以添加到下面的代码中吗?

 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."

1 个答案:

答案 0 :(得分: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