wordpad automation file printing

조회 수: 6 (최근 30일)
Alain Barraud
Alain Barraud 2021년 2월 11일
댓글: Alain Barraud 2021년 2월 15일
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에서 Use COM Objects in MATLAB에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by