sending a sequence of keystrokes in MS Word Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
stevewke
Posts: 1
Joined: 04 Oct 2017, 16:58

sending a sequence of keystrokes in MS Word

04 Oct 2017, 17:40

I am a total newbie to AHK with a very simple question. If I am editing in MS Word or Powerpoint, how can I use AHK to send a series of MS keyboard shortcuts as if I typed them. For example,
ALT (opens menu selection for all items on the ribbon)
H (opens Home Menu)
1 (selects BOLD)

Can someone point me to the syntax that does this or send me a code snippet or reference to do this?
Thanks
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: sending a sequence of keystrokes in MS Word  Topic is solved

04 Oct 2017, 17:49

Send command. For your example, it would be Send {Alt}h1 -- notice the h is uncapitalized. In the Send command, capital H counts as Shift+h which may result in a different action.

However, that may move too fast for Word to keep up with. You may need to either break the Send command into individual lines, one key per line, and put Sleep commands between the Send commands, or you can use SetKeyDelay.
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: sending a sequence of keystrokes in MS Word

04 Oct 2017, 19:13

Exaskryz wrote:However, that may move too fast for Word to keep up with. You may need to either break the Send command into individual lines, one key per line, and put Sleep commands between the Send commands, or you can use SetKeyDelay.
On the other hand, many times, this time included MS Word can handle the keyboard commands coming very quickly.

This line at the top of your script will make the Send command send the text pretty much as fast as possible.

Code: Select all

SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: sending a sequence of keystrokes in MS Word

04 Oct 2017, 23:01

You have to take into consideration the speed of the computer as well thus a break between them will ensure it will work. My computer can't handle it without sleep.
It switches slower and thus sends everything to fast.
FanaticGuru wrote:
Exaskryz wrote:However, that may move too fast for Word to keep up with. You may need to either break the Send command into individual lines, one key per line, and put Sleep commands between the Send commands, or you can use SetKeyDelay.
On the other hand, many times, this time included MS Word can handle the keyboard commands coming very quickly.

This line at the top of your script will make the Send command send the text pretty much as fast as possible.

Code: Select all

SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
FG
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: sending a sequence of keystrokes in MS Word

05 Oct 2017, 00:42

Johana wrote:You have to take into consideration the speed of the computer as well thus a break between them will ensure it will work. My computer can't handle it without sleep.
It switches slower and thus sends everything to fast.
That is not my experience at all with MS Word.

Code: Select all

SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.

F12::
	Loop, 50
		Send {Alt}h1
	MsgBox DONE
return
I push F12 and the DONE message pops up almost instantly but MS Word buffers the input and I can see 'Bold' toggling on and off for about 10 seconds. Long after the script sent all the info. MS Word buffers the keystrokes and processes them at its own speed.

Lots of programs that don't handle buffering of keystrokes, sure, lots of problems sending things too fast. Programs like MS Word that buffers properly, no problem.

At least that is my experience with MS Word on various computers with different versions of MS Word.

On the other side of the coin, everyone needs to do what works for them and from my experiment it does not matter how fast I send the keys it still takes MS Word about 200 ms to process the 'bold' toggle, so some small Sleeps are probably not really going to slow the process down. Sending really fast and buffering still has the advantage that normal user keystrokes go at the end of the buffer and do not get slipped in the middle of a string being sent by AHK.

SetKeyDelay can be useful if the program requires the key strokes to come in really slow like many games do. Unlike MS Word, they purposefully will not accept unhumanly fast keystrokes.

But really since I discovered the wonders of COM, I don't really send keystrokes to MS Word anymore.

Code: Select all

F12::
	ComObjActive("Word.Application").Selection.Font.Bold := 9999998
return
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Johana
Posts: 189
Joined: 02 May 2017, 02:34

Re: sending a sequence of keystrokes in MS Word

05 Oct 2017, 03:46

I am so new that never Heard about the COM. Sorry for hijacking the thread.

Regarding the Word: I am not sure it does buffer? We have "templates" in Word that we open where we have to change some things. To "jump" between where we need to change we press F11. And sometimes we need to press F11 5 times, then enter something, then press F11, then erase. Everything I've done with a GUI so it basically pastes variables.

If I don't have 300 sleep, it will input Everything so fast that Word doesn't have a chance. Everything gets on the first line..
Johana wrote:You have to take into consideration the speed of the computer as well thus a break between them will ensure it will work. My computer can't handle it without sleep.
It switches slower and thus sends everything to fast.
FanaticGuru wrote:
Exaskryz wrote:However, that may move too fast for Word to keep up with. You may need to either break the Send command into individual lines, one key per line, and put Sleep commands between the Send commands, or you can use SetKeyDelay.
On the other hand, many times, this time included MS Word can handle the keyboard commands coming very quickly.

This line at the top of your script will make the Send command send the text pretty much as fast as possible.

Code: Select all

SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
FG
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: sending a sequence of keystrokes in MS Word

05 Oct 2017, 15:26

Johana wrote:I am so new that never Heard about the COM. Sorry for hijacking the thread.

Regarding the Word: I am not sure it does buffer? We have "templates" in Word that we open where we have to change some things. To "jump" between where we need to change we press F11. And sometimes we need to press F11 5 times, then enter something, then press F11, then erase. Everything I've done with a GUI so it basically pastes variables.

If I don't have 300 sleep, it will input Everything so fast that Word doesn't have a chance. Everything gets on the first line..
For me on run of the mill office computers with MS Word 2010 & MS Word 365 running Windows 10, this works flawlessly:

Code: Select all

SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.

F12::
	Loop, 50
		Send {F11} Field Number: %A_Index%
return
It enters information in the first 50 fields of a large document. The script finishes in a faction of a second and then MS Word continues entering the information in the fields for another 6-8 seconds.

Could be a particularity of your script or a different software setup.

If you ever have any questions on how to do things with Word, Excel or Outlook through COM, you can generally get quite a bit of help from me or others on the forum. It is very powerful.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
RoyMarriott
Posts: 9
Joined: 14 Jan 2023, 13:15

Re: sending a sequence of keystrokes in MS Word

14 Jan 2023, 18:29

Sorry to resurrect this old thread... but I'm having a similar problem in 2023 (with AHK 2.0). In short, I can't get word to hear an Alt from AHK.

In more detail:
I'm trying to replicate the google shortcut to create a bulleted list in word (It's shift control 7)

I've got the following hotkey assignment:

Code: Select all

+^7::
{
	Send "{alt}"
	sleep 100
	Send "h"
	Sleep 100
	send "u"
	Sleep 100
	send "{up}"
	sleep 100
	send "{enter}"
}
If I press shift control 7, Word receives the h, u, up and enter fine, but never gets the alt.
If I press alt manually, then shift control 7, it works perfectly.

Any ideas how to get "alt" through to word?
RoyMarriott
Posts: 9
Joined: 14 Jan 2023, 13:15

Re: MS Word and AutoHotkey

14 Jan 2023, 18:47

replying to my own question a few minutes ago....

Send "!h"

...is the key - !h gets through where {alt}h doesn't.

(And it turns out that to toggle bullets, you need to do " send "!h{down}{right 10}{up}{enter}"" - but that's a quirk of the Word interface rather than anything related to AHK!)

Hope that helps anyone who finds themselves in the same situation....
gregster
Posts: 9002
Joined: 30 Sep 2013, 06:48

Re: MS Word and AutoHotkey

14 Jan 2023, 19:15

RoyMarriott wrote:
14 Jan 2023, 18:47
replying to my own question a few minutes ago....
Why in a different topic, though? I guess, it was just a mistake because your other post hadn't been approved yet.
So no worries, I have moved your second post to this topic here (where I found the first post).

PS:
Yes, ! will work as a modifier for the following key, while just sending {Alt} will already release the Alt key before sending the next key -> no modifier.
Alternatively, one could also try Send "{Alt down}h{Alt up}" (v2 syntax).
RoyMarriott
Posts: 9
Joined: 14 Jan 2023, 13:15

Re: MS Word and AutoHotkey

29 Jan 2023, 16:34

gregster wrote:
14 Jan 2023, 19:15
Why in a different topic, though? I guess, it was just a mistake because your other post hadn't been approved yet.
So no worries, I have moved your second post to this topic here (where I found the first post).
That's very helpful, thanks gregster
gregster wrote:
14 Jan 2023, 19:15
PS:
Yes, ! will work as a modifier for the following key, while just sending {Alt} will already release the Alt key before sending the next key -> no modifier.
Alternatively, one could also try Send "{Alt down}h{Alt up}" (v2 syntax).
Brilliant, that's just what I needed to know - thank you

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: doodles333, Google [Bot], mikeyww and 290 guests