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?
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.
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