TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Jim Tat
NA
52
47.2k
How to check if an MS Word is closed in C#
Sep 14 2016 5:42 AM
I am doing an application in C# which open exe files like notepad, calculator.<br /> So, my aim is when I open an exe file like Word, a button is created automatically<br /> and then when I close the Word app, the button will be removed. <br />Each app will have its own button when it is launched. <br />I tried to do it but I have some issues with the closing of the app. <br /><div>Here is what I did: </div><div> </div><div><div class="dp-highlighter"><ol class="dp-c" start="1"><li class="alt"><span><span>var applicationWord = </span><span class="keyword">new</span><span> Microsoft.Office.Interop.Word.Application(); </span></span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> applicationWord.Visible = <span class="keyword">true</span><span>; </span></span></li><li class="alt"><span> applicationWord.DocumentBeforeClose += DocumentBeforeClose; </span></li><li class=""><span> </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">if</span><span> (applicationWord.Visible == </span><span class="keyword">true</span><span>) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> button = <span class="keyword">new</span><span> Button(); </span></span></li><li class=""><span> button.Image = Properties.Resources.word_80; </span></li><li class="alt"><span> PIC_Barre.Controls.Add(button); </span></li><li class=""><span> button.AutoSize = <span class="keyword">true</span><span>; </span></span></li><li class="alt"><span> <span class="comment">// button.Tag = proc.Id;</span><span> </span></span></li><li class=""><span> PIC_Barre.Controls.Add(button); </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">foreach</span><span> (Process proc </span><span class="keyword">in</span><span> Process.GetProcessesByName(</span><span class="string">"WINWORD"</span><span>)) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> <span class="keyword">if</span><span> (proc.ProcessName.Contains(</span><span class="string">"WINWORD"</span><span>)) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> proc.WaitForInputIdle(); </span></li><li class=""><span> { </span></li><li class="alt"><span> Thread.Sleep(500); </span></li><li class=""><span> SetWindowPos(proc.MainWindowHandle.ToInt32(), </span></li><li class="alt"><span> (<span class="keyword">int</span><span>)SetWinPos_ZOrderOpt.HWND_TOPMOST, </span></span></li><li class=""><span> 0, 0, 0, 0, </span></li><li class="alt"><span> (<span class="keyword">int</span><span>)(SetWinPosFlags.SWP_NOSIZE | </span></span></li><li class=""><span> SetWinPosFlags.SWP_NOMOVE)); </span></li><li class="alt"><span> } </span></li><li class=""><span> button.Click += (s, e) => { ShowWindowAsync(proc.MainWindowHandle, (<span class="keyword">int</span><span>)ShowWindowCommands.Normal); }; </span></span></li><li class="alt"><span> proc.Exited += (s, e) => </span></li><li class=""><span> { </span></li><li class="alt"><span> var method = (Action)(() => PIC_Barre.Controls.Remove(button)); </span></li><li class=""><span> <span class="comment">// button.Visible = false;</span><span> </span></span></li><li class="alt"><span> <span class="keyword">if</span><span> (button.InvokeRequired) </span></span></li><li class=""><span> { </span></li><li class="alt"><span> button.Invoke(method); </span></li><li class=""><span> } </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> }; </span></li><li class=""><span> </span></li><li class="alt"><span> } </span></li><li class=""><span> </span></li><li class="alt"><span> } </span></li><li class=""><span> } <br /></span></li></ol></div> </div>
Reply
Answers (
5
)
How To Convert Shape to Image From Excel using NPOI and C#?
How can I add arraylist items in Datatable?