Vb.Net Printing File to Printer

andyb7901
andyb7901 used Ask the Experts™
on
Hey,

Does anyone know how to send a file directly to the printer, no matter what the file type is? I have a set of files currently that I double click on and they are sent directly to the printer. Or I am asked to define the file type. Can this be done in VB. Either printing direct to the printer, or performing a double click type scenario on the file through VB?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
If Windows knows how to print the document, you can use the Process component to print it. See http://www.emoreau.com/Entries/Articles/2003/12/The-Process-component.aspx

Author

Commented:
I have created a little test folder to try and run this. I have tried the following;

MyProcess.StartInfo.FileName = "c:\hello\hello" - Hello delibrately has not file type.

I get en error though;

No application is associated with the specified file for this operation
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
The file needs an association otherwise the Windows shell (used by the process component) won't know how to print it.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
I have tried to play around with your code and tried the following;

        MyProcess.StartInfo.FileName = "C:\xPrint\vpxPrint.exe"
        MyProcess.StartInfo.WorkingDirectory = "c:\hello"
        MyProcess.StartInfo.Arguments = "Hello.*"
        MyProcess.StartInfo.Verb = "Print"
        MyProcess.Start()

Can I associate all records that I want to print with the xPrint program? Is there a way to open files within certian applications?

Author

Commented:
p.s. - the above code did not work?
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
>>MyProcess.StartInfo.FileName = "C:\xPrint\vpxPrint.exe"

FileName is the name of the to print and not the name of the application used to print.

>>Can I associate all records that I want to print with the xPrint program?

It has to be done in Windows itself.

>>p.s. - the above code did not work?

If you take the code from the article and you try to print a file that is already associated, it will work.

Author

Commented:
No, I didnt mean your code didn't work, I meant my revised code.

Basically I need to open files that do not have file associations. I need to do this through VB. What could you suggest? All the files need to opened through my xPrint application "C:\xPrint\vpxPrint.exe".
Senior .Net Consultant
Top Expert 2016
Commented:
can the vpxPrint.exe accepts a filename as the arguments? does it automatically print the file pass as an argument?

if this is the case, the syntax you need to use is:
MyProcess.StartInfo.FileName = "C:\xPrint\vpxPrint.exe"
MyProcess.StartInfo.Arguments = "c:\hello."
MyProcess.Start()

Author

Commented:
At the moment if i go to my file, right click and use the "Open With" option from windows, I browse to the vpxPrint.exe. Once I have selected this it opens perfectly. With your code I get the error;

"unknown input file C:\hello"

If this does not work is there anyway to save the current file name in a variable and then rename the file it so it includes the file extension. Once I have finished with the file rename it back to its orignal name?

Author

Commented:
I have it working on the C: file now. However, when i do it on one of my real files it doesnt seem to like spaces. I have the following file to be read into the xPrint file;

"c:\hello\SUPPLIER NOTIFICATION-3177-0504200747522"

However it only seems to read NOTIFICATION-3177-0504200747522 and not the Supplier bit before that? any reason why?
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
have you tried:
MyProcess.StartInfo.Arguments = "c:\hello."
MyProcess.StartInfo.Arguments = "c:\hello"

Author

Commented:
I now have it working using;

Dim MyProcess As New Process
MyProcess.StartInfo.FileName = "C:\xPrint\vpxPrint.exe"
MyProcess.StartInfo.Arguments = """c:\hello\SUPPLIER NOTIFICATION-3177-0504200747522"""
MyProcess.Start()

Thanks for all your great help!
Éric MoreauSenior .Net Consultant
Top Expert 2016

Commented:
for the space, surround your file name with double quotes:

MyProcess.StartInfo.Arguments = """"  & "c:\hello" & """"

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial