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
47k
How to close a running instance of MS Word in C#
Sep 19 2016 11:26 AM
I am doing an application in which when I launch a MS Word app,<br /> a button is created and when the MS Word app is closed the button is removed.<br /><div> So here is what I did to associate the MS Word and the button...</div><div> </div><div>Here is the code: </div><div><div class="dp-highlighter"><ol class="dp-c" start="1"><li class="alt"><span><span class="keyword">try</span><span> </span></span></li><li class=""><span> { </span></li><li class="alt"><span> </span></li><li class=""><span> Word.Application wdApp = <span class="keyword">new</span><span> Word.Application(); </span></span></li><li class="alt"><span> <span class="keyword">string</span><span> oldCaption = wdApp.Application.Caption; </span></span></li><li class=""><span> <span class="keyword">string</span><span> guid = Guid.NewGuid().ToString(); </span></span></li><li class="alt"><span> <span class="comment">//set caption to random value</span><span> </span></span></li><li class=""><span> wdApp.Application.Caption = guid; </span></li><li class="alt"><span> <span class="comment">//make sure app is visible:</span><span> </span></span></li><li class=""><span> wdApp.Visible = <span class="keyword">true</span><span>; </span></span></li><li class="alt"><span> <span class="comment">//find random value to get process id</span><span> </span></span></li><li class=""><span> <span class="keyword">int</span><span> processId = GetProcessIdByWindowTitle(guid); </span></span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">//reset caption</span><span> </span></span></li><li class=""><span> wdApp.Application.Caption = oldCaption; </span></li><li class="alt"><span> </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="keyword">if</span><span>( wdApp.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> deleteButton = <span class="keyword">new</span><span> Microsoft.Office.Tools.Word.Controls.Button(); </span></span></li><li class="alt"><span> <span class="comment">//create a dictionary</span><span> </span></span></li><li class=""><span> mapping = <span class="keyword">new</span><span> Dictionary<</span><span class="keyword">int</span><span>, Button>(); </span></span></li><li class="alt"><span> <span class="comment">//add mapping</span><span> </span></span></li><li class=""><span> mapping.Add(processId, deleteButton); </span></li><li class="alt"><span> PIC_Barre.Controls.Add(deleteButton); </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 class="comment">//PIC_Barre.Controls.Add(_button);</span><span> </span></span></li><li class="alt"><span> } </span></li><li class=""><span> ((Word.ApplicationEvents4_Event)wdApp).Quit += () => </span></li><li class="alt"><span> { </span></li><li class=""><span> </span></li><li class="alt"><span> <span class="comment">// remove the button corresponding to the processid</span><span> </span></span></li><li class=""><span> var method = (Action)(() => </span></li><li class="alt"><span> PIC_Barre.Controls.Remove(mapping[processId])); </span></li><li class=""><span> <span class="keyword">if</span><span> (mapping[processId].InvokeRequired) </span></span></li><li class="alt"><span> { </span></li><li class=""><span> mapping[processId].Invoke(method); </span></li><li class="alt"><span> </span></li><li class=""><span> } </span></li><li class="alt"><span> <span class="comment">// remove the key from the dictionary</span><span> </span></span></li><li class=""><span> </span></li><li class="alt"><span> }; </span></li><li class=""><span> Debugger.Break(); </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 class="keyword">catch</span><span> </span></span></li><li class=""><span> { </span></li><li class="alt"><span> </span></li><li class=""><span> } <br /></span></li></ol></div> I can remove only the last button I create, when I want to remove the others I can't... I put breakpoint in order to know the value of the different variables .<br /> But when I create 2 buttons, the count in the mapping.add is always equal to 1... Is it normal ? </div>
Reply
Answers (
1
)
Mathematical Operator in an expression
Autocomplet word (search)