wordpad automation file printing

8 views (last 30 days)
My problem is the following. I want to print from matlab to the default windows printer a text file created with matlab.
I have successfully open the file using
system("C:\Program Files (x86)\Windows NT\Accessories\wordpad.exe & myfile.txt")
I can manually print the file and exit wordpad. My question is how can I programmatically open the file print and quit wordpad in a siingle command?
A single command because once system("C:\Program Files (x86)\Windows NT\Accessories\wordpad.exe & myfile.txt") is done matlab is waiting until wordpad is closed.
Another way may be to use activeX with word. This initial choice is to avoid end user to have microsoft office.
Thanks a lot for any suggestion.

Accepted Answer

Mario Malic
Mario Malic on 11 Feb 2021
Edited: Mario Malic on 15 Feb 2021
Hi Alain,
here's the partial answer, see the Internet Explorer way.The issue is that you'll get the printer dialog box in which you'll have to press the button to print manually.
If you're able to somehow find the way to do it automatically, it'd be preety cool. There are few topics on web that are concerned with this dialog box, but in other environments, so if you're brave enough, try them in MATLAB.
There is another way with notepad, see arguments here.
Sending keystrokes to the opened application, also useful but due to the fact that printing occurs in another window, sending enter won't close the dialog box. See here.
Edit so the answer is on top:
WordPadProcess = System.Diagnostics.Process;
WordPadProcess.StartInfo.Arguments = "/p filename.txt"
WordPadProcess.StartInfo.FileName = "C:\Program Files (x86)\Windows NT\Accessories\wordpad.exe";
WordPadProcess.StartInfo.UseShellExecute = 0; % you get the error below if run with true
while ~WordPadProcess.HasExited; pause(0.5); end % MATLAB won't proceed further until WordPad is closed
%Message: The Process object must have the UseShellExecute property set
%to false in order to use environment variables.
If one wants to close the process, it can be done with
Alain Barraud
Alain Barraud on 15 Feb 2021
I have done the following modification in order to accept file name as a variable
function PrintTextFile(FileName)
WordPadProcess = System.Diagnostics.Process;
FileName=["/p ",FileName];FileName=string(cat(2,FileName{:}));
WordPadProcess.StartInfo.Arguments = FileName;
WordPadProcess.StartInfo.FileName = "C:\Program Files (x86)\Windows NT\Accessories\wordpad.exe";
WordPadProcess.StartInfo.UseShellExecute = 0; % you get an error when true !!
while ~WordPadProcess.HasExited; pause(0.5); end

Sign in to comment.

More Answers (0)




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by