vba 在 Visual Basic 中使用记事本和命令行获取要打印到特定打印机的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18492442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting a file to print to a specific printer using notepad and command line in visual basic
提问by Michael Wilson
I have been trying to get a file to print to a specific printer using notepad. I have managed to get it to work when I set the printer in question to the default printer.
我一直在尝试使用记事本将文件打印到特定的打印机。当我将有问题的打印机设置为默认打印机时,我设法让它工作。
This is the code I used to achieve this:
这是我用来实现此目的的代码:
Shell("NOTEPAD /P C:\Temp\test.txt")
The issue i've got is that I need to send the file to this printer when it is not the default printer.
我遇到的问题是,当它不是默认打印机时,我需要将文件发送到这台打印机。
回答by Ken White
Some applications (including Notepad) support a printto
command. Notepad's is /pt printername
. You'll probably have to experiment some to get the printer name just right - I believe it's the name of the printer as seen in Control Panel, but it may be the name of the device or driver itself. (A few quick tests should help you figure out which applies.)
某些应用程序(包括记事本)支持printto
命令。记事本是/pt printername
. 您可能需要尝试一些方法才能使打印机名称恰到好处 - 我相信这是在控制面板中看到的打印机名称,但它可能是设备或驱动程序本身的名称。(一些快速测试应该可以帮助您确定哪些适用。)
Shell("NOTEPAD /PT MyLaserJet C:\Temp\test.txt")
Of course, the proper solution to this problem is to change your application so it doesn't use Shell("notepad",...)
to do the printing, but actually sends the text to the printer itself. You can then have the user set up the printer once, save that configuration, and then automatically send the text to the proper printer every time. Using an external application to do the work your app should do itself is a workaround, not a solution. :-) I can't suggest how you would do that, because you've provided no information about your application. There are lots of questions here related to printing for almost every language and platform, though - you can probably find one that will get you started with a little searching.
当然,此问题的正确解决方案是更改您的应用程序,使其不用于Shell("notepad",...)
打印,而是实际将文本发送到打印机本身。然后,您可以让用户设置打印机一次,保存该配置,然后每次都自动将文本发送到正确的打印机。使用外部应用程序来完成您的应用程序应该自己完成的工作是一种变通方法,而不是解决方案。:-) 我不能建议你怎么做,因为你没有提供关于你的应用程序的信息。不过,这里有很多与几乎所有语言和平台的打印相关的问题 - 您可能会找到一个可以让您开始进行一些搜索的问题。
回答by arrgvMatey
I could not get the above to work for me from excel 2013. don't know why.
我无法从 excel 2013 中获得上述内容。不知道为什么。
The following worked for me:
以下对我有用:
Shell ("NOTEPAD /PT " & Chr(34) & "filename" & Chr(34) & Chr(34) & "printer name" & Chr(34))
Shell ("NOTEPAD /PT" & Chr(34) & " filename" & Chr(34) & Chr(34) & "打印机名称" & Chr(34))