Avatar of andyb7901
andyb7901
 asked on

Vb.Net Printing File to Printer

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?
Visual Basic.NETVisual Basic Classic

Avatar of undefined
Last Comment
Éric Moreau

8/22/2022 - Mon
Éric Moreau

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
andyb7901

ASKER
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 Moreau

The file needs an association otherwise the Windows shell (used by the process component) won't know how to print it.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
andyb7901

ASKER
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?
andyb7901

ASKER
p.s. - the above code did not work?
Éric Moreau

>>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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
andyb7901

ASKER
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".
ASKER CERTIFIED SOLUTION
Éric Moreau

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
andyb7901

ASKER
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?
andyb7901

ASKER
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?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Éric Moreau

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

ASKER
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 Moreau

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

MyProcess.StartInfo.Arguments = """"  & "c:\hello" & """"
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.