【发布时间】:2019-05-10 14:58:58
【问题描述】:
在 Excel 中打印时,我的工作场所有一个额外的弹出窗口,用于选择打印选项。它不是 Excel 的一部分(我相信它是佳能打印机对话窗口)。这些选项允许您指定以彩色、装订和分页等方式打印。它们不是 excel 打印选项。
过去,我使用过一个宏,它使用SendKeys 复制用于选择(在 Excel 中)页面布局 (alt P)、页面设置 (alt I) 和“选项”的键盘快捷键页面设置屏幕 (alt O)。选择“选项”后,打印机对话屏幕打开,宏继续使用SendKeys 在此窗口中选择配置文件(每个配置文件都包含彩色打印、装订和分页等选项)。这段代码如下:
Sub Test()
Application.SendKeys ("%p"), True 'Selects Page Layout
Application.SendKeys ("%i"), True 'Selects Print Titles
Application.SendKeys ("%o"), True 'Selects Options
Application.SendKeys ("p"), True 'Selects 'Portrait' default (this needs to be set up initially)
Application.SendKeys "{TAB 19}", True 'Tabs to OK
Application.Wait (Now() + TimeValue("00:00:01"))
Application.SendKeys "~", True 'Hits enter to close screen
Application.Wait (Now() + TimeValue("00:00:01"))
Application.SendKeys "~", True 'Hits enter to close screen
End Sub
自从迁移到 Windows 10/Office 2016 - SendKeys 在单独的打印机窗口打开时失败(特别是在以 Application.SendKeys ("p"), True 开头的行及以后)。基本上,宏会打开打印机设置窗口,但之后什么都不做。
我曾尝试寻找 SendKeys 的替代品,但我很难理解如何通过 VBA 自动执行点击 p 的过程(在打印对话窗口中选择纵向配置文件),点击标签 19 次(到进入退出屏幕按钮),然后按两次 Enter(关闭后续对话窗口 - 这是 excel 窗口)。需要明确的是 - 提到的“纵向”配置文件是一个特定的打印机配置文件,它指定了许多选项,包括方向、2 面打印、装订位置、颜色模式和装订/分页/组首选项。
如果可能的话,我很乐意替换所有 SendKeys 命令,因为我知道它们不可靠/不受支持。
[更新 14.05.2019]:
所以我考虑尝试用“Keybd_Event”替换发送键,但这遇到了完全相同的障碍(在打印机对话窗口打开之前一直有效)。
[2019 年 5 月 20 日更新]
@Selkie 的解决方案有效,我已将其标记为答案。
这是我最后使用的代码,尽管仍然需要对其进行调整,以便循环遍历选定的工作表:
Sub PrinterSetUp()
Dim filepath As String
Dim Msg As Integer
'Filepath can't have a space in it
filepath = "Directory\PrinterScriptPortrait.vbs"
If Dir(filepath) <> "" Then
'Hurray it exists
Else
'It doesn't exist yet, create the file
WriteVBSScript (filepath)
End If
Shell "wscript " & filepath, vbNormalFocus
'no code after here, otherwise everything breaks.
End Sub
Sub WriteVBSScript(filepath As String)
Dim VBScriptString As String
Dim fso As Object
Dim oFile As Object
'Needs to be done as a VBS script because VBA and sendkeys don't play well with dialog boxes.
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.CreateTextFile(filepath)
VBScriptString = VBScriptString & "Set WSHShell = WScript.CreateObject( " & Chr(34) & "WScript.Shell" & Chr(34) & " )" & vbNewLine
VBScriptString = VBScriptString & "WSHshell.AppActivate " & Chr(34) & " Excel.exe " & Chr(34) & " " & vbNewLine
VBScriptString = VBScriptString & "wsh.sleep 1000" & vbNewLine
VBScriptString = VBScriptString & "WSHshell.SendKeys " & Chr(34) & "%p" & Chr(34) & " " & vbNewLine
VBScriptString = VBScriptString & "wsh.sleep 1000" & vbNewLine
VBScriptString = VBScriptString & "WSHshell.SendKeys " & Chr(34) & "%i" & Chr(34) & " " & vbNewLine
VBScriptString = VBScriptString & "wsh.sleep 1000" & vbNewLine
VBScriptString = VBScriptString & "WSHshell.SendKeys " & Chr(34) & "%o" & Chr(34) & " " & vbNewLine
VBScriptString = VBScriptString & "wsh.sleep 1000" & vbNewLine
VBScriptString = VBScriptString & "WSHshell.SendKeys " & Chr(34) & "p" & Chr(34) & " " & vbNewLine
VBScriptString = VBScriptString & "wsh.sleep 1000" & vbNewLine
VBScriptString = VBScriptString & "WSHshell.SendKeys " & Chr(34) & "{TAB 19}" & Chr(34) & " " & vbNewLine
VBScriptString = VBScriptString & "wsh.sleep 1000" & vbNewLine
VBScriptString = VBScriptString & "WSHshell.SendKeys " & Chr(34) & "~" & Chr(34) & " " & vbNewLine
VBScriptString = VBScriptString & "wsh.sleep 1000" & vbNewLine
VBScriptString = VBScriptString & "WSHshell.SendKeys " & Chr(34) & "~" & Chr(34) & " " & vbNewLine
VBScriptString = VBScriptString & "wsh.sleep 1000" & vbNewLine
oFile.WriteLine VBScriptString
oFile.Close
Set fso = Nothing
Set oFile = Nothing
End Sub
【问题讨论】:
-
不清楚如何触发打印:使用 VBA 代码还是手动按下标准 excel 按钮?如果 VBA 显示代码。
-
打印由 VBA 触发 - 上面的 sendkeys 语句,即“选择选项”是打开打印机对话窗口的方式 - sendkeys 从字面上复制您将使用的键盘快捷键命令。我会修改我的问题。
-
如果您使用
ActiveSheet.PrintOut之类的东西触发打印会发生什么?你看到佳能对话框了吗? -
您愿意使用其他语言吗?可能会有些不同,但您可以使用称为 Autohotkey 的语言更轻松地自动填写表单。