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

MEUserII

Board Regular
Joined
Oct 27, 2017
Messages
59
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>
 

Macropod

Retired Moderator
Joined
Aug 27, 2007
Messages
3,312
Quite clearly, WshShell.Sendkeys is not the same as Wsh.Shell.SendKeys...
 

MEUserII

Board Regular
Joined
Oct 27, 2017
Messages
59
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:

MEUserII

Board Regular
Joined
Oct 27, 2017
Messages
59
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:

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
5,995
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/17038282/press-keyboard-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.
 

Forum statistics

Threads
1,081,677
Messages
5,360,451
Members
400,586
Latest member
Minty

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top