wordpad automation file printing

4 vues (au cours des 30 derniers jours)
Alain Barraud
Alain Barraud le 11 Fév 2021
Commenté : Alain Barraud le 15 Fév 2021
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.

Réponse acceptée

Mario Malic
Mario Malic le 11 Fév 2021
Modifié(e) : Mario Malic le 15 Fév 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
WordPadProcess.Start();
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
WordPadProcess.Kill;
  7 commentaires
Mario Malic
Mario Malic le 15 Fév 2021
Great!
It is part of .NET framework, here's documentation on it.
You can close the process by this line as well (I glanced over at the Methods, learned something new).
WordPadProcess.Kill;
Alain Barraud
Alain Barraud le 15 Fév 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 !!
WordPadProcess.Start();
while ~WordPadProcess.HasExited; pause(0.5); end

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB Compiler dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by