Batch Script - Send Keys Inside of Batch Script to Excel?

MEUserII

Board Regular
Joined
Oct 27, 2017
Messages
91
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
Platform
  1. Windows
Is there a way to send keys inside of a batch script (.bat file) on Windows 10 to Excel 2016? For example, this link below provides an example on how to send keys in a batch script (.bat) to Word using the following commands:

Link: https://itstillworks.com/12456076/how-to-press-buttons-in-batch-file
Code:
<code class="language-markup">
cd C:\Program Files (x86)\Microsoft Office\Office14</code><code class="language-markup">
start /w WinWord.exe</code>
<code class="language-markup"></code><code class="language-markup"></code><code class="language-markup"><code class="language-markup">WshShell.Sendkeys "%f"</code></code>
<code class="language-markup"><code class="language-markup"></code></code><code class="language-markup"><code class="language-markup"><code class="language-markup">WshShell.Sendkeys “n”</code></code></code>
<code class="language-markup"><code class="language-markup"><code class="language-markup"></code></code></code><code class="language-markup"></code><code class="language-markup"><code class="language-markup"><code class="language-markup"><code class="language-markup">WshShell.Sendkeys "{Enter}"

However, this does not seem to be working for me. As I get following error:
'Wsh.Shell.SendKeys' is not recognized as an internal or external command, operable program or batch file."

So, how would I be able to send keys in a batch script on Windows 10 to Excel 2016?
</code></code></code></code>
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Quite clearly, WshShell.Sendkeys is not the same as Wsh.Shell.SendKeys...
Thank you for pointing that out in your suggestion.

Specifically, I tried both variations of that command pointed out: WshShell.Sendkeys and WshShell.Sendkeys; and neither worked in my batch script (.bat file). For reference, I have listed the code for both attempts as well as their respective error codes.

Attempted Code 1:
Code:
<code class="language-markup">cd C:\Program Files (x86)\Microsoft Office\Office14</code><code class="language-markup"></code><code class="language-markup">
start /w WinWord.exe</code><code class="language-markup"><code class="language-markup">
Wsh.Shell.Sendkeys "%f"</code></code> <code class="language-markup"></code><code class="language-markup"><code class="language-markup"><code class="language-markup">
Wsh.Shell.Sendkeys “n”</code></code></code><code class="language-markup"><code class="language-markup"><code class="language-markup"><code class="language-markup">
Wsh.Shell.Sendkeys "{Enter}"</code></code></code></code>
<code class="language-markup"><code class="language-markup"><code class="language-markup"></code></code></code>

This first code yields the following error message:
'Wsh.Shell.Sendkeys' is not recognized as an internal or external command, operable program or batch file.

Attempted Code 2:
Code:
<code class="language-markup"></code><code class="language-markup">cd C:\Program Files (x86)\Microsoft Office\Office14</code><code class="language-markup"></code><code class="language-markup"></code>
<code class="language-markup"></code><code class="language-markup">start /w WinWord.exe</code><code class="language-markup"><code class="language-markup"></code></code>
<code class="language-markup"><code class="language-markup"></code></code><code class="language-markup"><code class="language-markup">WshShell.Sendkeys "%f"</code></code> <code class="language-markup"></code><code class="language-markup"><code class="language-markup"><code class="language-markup">
WshShell.Sendkeys “n”</code></code></code><code class="language-markup"><code class="language-markup"><code class="language-markup"><code class="language-markup"></code></code></code></code>
<code class="language-markup"><code class="language-markup"><code class="language-markup"><code class="language-markup"></code></code></code></code><code class="language-markup"><code class="language-markup"><code class="language-markup"><code class="language-markup">WshShell.Sendkeys "{Enter}"</code></code></code></code>
<code class="language-markup"><code class="language-markup"><code class="language-markup"></code></code></code>
<code class="language-markup"><code class="language-markup"></code></code><code class="language-markup"><code class="language-markup"></code></code>
This second code yields the following error message:
'WshShell.Sendkeys' is not recognized as an internal or external command, operable program or batch file.

So, would anyone know how to be able to send keys in a batch script on Windows 10 to Excel 2016?
 
Last edited:
Upvote 0
So, would anyone know how to be able to send keys in a batch script on Windows 10 to Excel 2016 because I am still stuck on this?
 
Last edited:
Upvote 0
So, would anyone know how to be able to send keys in a batch script on Windows 10 to Excel 2016 because I am still stuck on this?
See https://stackoverflow.com/questions/25371406/how-to-press-a-key-with-batch-file and https://stackoverflow.com/questions...ard-keys-using-a-batch-file/17050135#17050135.

Note - your code starts Word, not Excel. For Excel change WinWord.exe to Excel.exe.

Does it need to be a batch script? The examples shown in the links have VBScript/JScript code embedded in a batch file. It would be easier to just have a VBScript (.vbs file) which starts Excel and presses the keys, using WScript.Shell SendKeys.

But what, exactly, are you trying to do? There might be an easier way than sending key presses, which is usually unreliable.
 
Upvote 0

Forum statistics

Threads
1,214,657
Messages
6,120,769
Members
448,991
Latest member
Hanakoro

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top