【发布时间】:2012-01-26 16:28:09
【问题描述】:
我需要执行的步骤是重复的,但我不确定如何遍历每个工作簿,然后是每个工作表。
我的任务是:
- 查看文件夹:(源文件夹)
- 遍历该文件夹中的每个工作簿(源文件)
- 将每个工作簿中的 4 个工作表(工作表名称)分别打印到 PostScript 打印机(打印机名称/路径)。
- 将打印文件命名为 PS 文件 = 源文件+图纸名称
- 最终 PS 输出文件放置在最终文件夹(目标文件夹)中
- 原始工作簿已关闭且未保存。
我搜索了迭代 VBA/Macro 并看到了一些想法,但我不确定代码在处理工作簿和工作表时的外观。
另外,PS 打印机是通过打印到文件完成的。这会导致问题吗?
更新了我迄今为止尝试过的代码:
Sub Make_PS_Files()
Dim path2 As String, path3 As String
path2 = "Drive:\Source folder\"
path3 = " Drive:\Destination folder\"
Workbooks.Open Filename:=path2 + "File_Name.XLS"
Sheets("Specific_Sheet_Name1").Activate
Application.ActivePrinter = "\\PRINTER NAME\LOCATION:"
ActiveWindow.SelectedSheets.PrintOut copies:=1, PrintToFile:=True, Collate _
:=True, Prtofilename:=True
ActiveSheet.PrintOut copies:=1, Prtofilename:=path3 + " Specific_Sheet_Name1.ps"
Sheets("Specific_Sheet_Name2").Activate
Application.ActivePrinter = "\\VS PRINTER NAME\LOCATION:"
ActiveWindow.SelectedSheets.PrintOut copies:=1, PrintToFile:=True, Collate _
:=True, Prtofilename:=True
ActiveSheet.PrintOut copies:=1, Prtofilename:=path3 + " Specific_Sheet_Name2.ps"
Sheets("Specific_Sheet_Name3").Activate
Application.ActivePrinter = "\\ PRINTER NAME\LOCATION:"
ActiveWindow.SelectedSheets.PrintOut copies:=1, PrintToFile:=True, Collate _
:=True, Prtofilename:=True
ActiveSheet.PrintOut copies:=1, Prtofilename:=path3 + " Specific_Sheet_Name3.ps"
ActiveWorkbook.Close
Sheets("Specific_Sheet_Name4").Activate
Application.ActivePrinter = "\\ PRINTER NAME\LOCATION:"
ActiveWindow.SelectedSheets.PrintOut copies:=1, PrintToFile:=True, Collate _
:=True, Prtofilename:=True
ActiveSheet.PrintOut copies:=1, Prtofilename:=path3 + " Specific_Sheet_Name4.ps"
ActiveWorkbook.Close
End Sub
抱歉,昨晚我发帖时没有发帖。它正在做的事情似乎很啰嗦。我认为它可以再抛光一点,这样它就更通用了,可以指向任何工作簿和任意数量的工作表。
并非所有工作表都有Specific_Sheet_Name,所以我想在不参考名称的情况下进行迭代。
【问题讨论】:
-
@Jmax:很抱歉没有发布代码。我已经添加了上面的内容。
-
@Mike,您实际上是在为每张纸使用不同的打印机吗?如果没有,那么您只需要设置一次
Application.ActivePrinter。此外,您应该在更改之前保存ActivePrinter的值,然后在完成时(或发生错误时)重置它。 -
@Harry。保存活动打印机的价值?我通过记录宏找到活动打印机的值,然后粘贴适当的值。你如何保存/重置它?为什么?
-
@Mike 您可以通过将其值存储在变量中来保存它。如果您选择的打印机不是用户的默认打印机,如果他们尝试在该 Excel 实例中手动打印,他们会感到惊讶。它不会使用他们的系统默认值,而是使用您选择的那个。
-
@Harry。谢谢。我会调查一下并试一试。