getting a file to print to a specific printer using notepad and command line in visual basic

StackOverflow https://stackoverflow.com/questions/18492442

Pregunta

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.

¿Fue útil?

Solución

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

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.

Otros consejos

I could not get the above to work for me from excel 2013. don't know why.

The following worked for me:

Shell ("NOTEPAD /PT " & Chr(34) & "filename " & Chr(34) & Chr(34) & "printer name" & Chr(34))

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top