wordpad automation file printing

14 次查看(过去 30 天)
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.

采纳的回答

Mario Malic
Mario Malic 2021-2-11
编辑:Mario Malic 2021-2-15
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 个评论
Mario Malic
Mario Malic 2021-2-15
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 2021-2-15
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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Compiler 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by