How to send CTRL+S to MS Word?

How to send CTRL+S to MS Word?



I am working on a program that auto saves a Microsoft Office document after specific time (loop).



On a click of a button it will activate timer and after a given time delay it should send CTRL+S to Word and save the document.


CTRL+S



Here is my code:


private void button1_Click(object sender, EventArgs e)

startTimer.Enabled = true;
stratTimer.Start();


private void startTimer_Tick(object sender, EventArgs e)

SendKeys.SendWait("^s");
MessageBox.Show("doc auto saved");



The problem is that SendKeys.Send("^s"); does not work.


SendKeys.Send("^s");



How can I accomplish that?






Ever since Windows Vista, apps should not attempt to send keystrokes via message pump or other legacy means. Look into Windows UI Automation instead

– MickyD
Sep 14 '18 at 2:00






Additionally you can use Visual Studio Tools for Office and use the .NET wrappers around the COM Automation model

– MickyD
Sep 14 '18 at 2:16






What is your goal here? Word already has an option to auto save at a specified interval.

– BJ Myers
Sep 14 '18 at 2:59




1 Answer
1



SendKeys.SendWait sends keystrokes to the currently active window, so the keystrokes can be received by other window, like browser (if you are viewing this answer while the automation exe is running in the background). It is not a wise idea to send key strokes to arbitrary window.


SendKeys.SendWait



This can partially explain why your code does not work in some cases. And it has other issues.



Just as MickyD's comment, don't use this "SendKeys" solution for automation nowadays. For auto save, VSTO is the right way to go.


MickyD



See How to: Programmatically save documents.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)