VBA: SendKeys for the Mac

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
21,586
Office Version
  1. 365
Platform
  1. Windows
Can someone please confirm for me whether or not the 'SendKeys' statement is available for the Mac (Excel v.X). When I try something as simple as...

Code:
SendKeys "ABC"

...I receive the following error...

Code:
"Run-time error '5':

Invalid procedure call or argument"

Thanks!
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I assume it must be because there is mention of it in help.

Note You can't use SendKeys to send keystrokes to an application that is not designed to run in Microsoft Windows or Macintosh. Sendkeys also can't send the PRINT SCREEN key {<CODE>PRTSC</CODE>} to any application.

Although the syntax you have shown seems fine!

Maybe you need Application.SendKeys?
 
Last edited:
Upvote 0
I assume it must be because there is mention of it in help.



Although the syntax you have shown seems fine!

Maybe I'm misunderstanding the nature SendKeys Statement. I've placed that statement within a sub procedure and tried running it while my file is active. As far as I understand, the text "ABC" (in my example) should be typed to the active window. Is this correct?

Maybe you need Application.SendKeys?

For the SendKeys Method, it specifically states that this command is not available for the Mac. I receive the following error...

Code:
"Run-time error 1004:

Command not available in Microsoft Excel for the Macintosh."
 
Last edited:
Upvote 0
Maybe I'm misunderstanding the nature SendKeys Statement. I've placed that statement within a sub procedure and tried running it while my file is active. As far as I understand, the text "ABC" (in my example) should be typed to the active window. Is this correct?

Yes absolutely correct. If I run the following directly from the VBE it places the same text in at the cursor position:

Code:
[COLOR=blue]Sub[/COLOR] phoo()
    [COLOR=blue]SendKeys[/COLOR] "ABC"
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]


Consider getting in touch with Mike Rickson. He may be aware of this particular issue.

I know nothing about Mac but I see there are articles online suggesting API sendkeys alternatives. Perhaps one of these could work for you.

P.S - just had a look at your website. Looking good, nice content! There is very little in the way of formula out there (compared with VBA) so nice that you are covering it so well!
 
Upvote 0
Yes absolutely correct. If I run the following directly from the VBE it places the same text in at the cursor position:

Code:
[COLOR=blue]Sub[/COLOR] phoo()
    [COLOR=blue]SendKeys[/COLOR] "ABC"
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]

Thanks for confirming this for me, Jon. Much appreciated.

I know nothing about Mac but I see there are articles online suggesting API sendkeys alternatives. Perhaps one of these could work for you.

Great, I'll have to check into it...

P.S - just had a look at your website. Looking good, nice content! There is very little in the way of formula out there (compared with VBA) so nice that you are covering it so well!

Thanks, Jon. That's nice of you to say...
 
Upvote 0
You can't use APIs because they are actually Windows API functions.
There is probably an AppleScript equivalent for whatever you are trying to do though.
(IMO, SendKeys should be removed from VBA altogether)
 
Upvote 0
You can't use APIs because they are actually Windows API functions.

Oh, I see... Thanks Rory.

Code:
There is probably an AppleScript equivalent for whatever you are trying to do though.

No doubt...

(IMO, SendKeys should be removed from VBA altogether)

Interesting... Why do you say that?
 
Upvote 0
Because it's unreliable and kludgy (technical term!). Mostly when I see people use it, there are better ways to do what they want, though I accept that sometimes there aren't; for those occasions the API should suffice.
 
Upvote 0
Because it's unreliable and kludgy (technical term!). Mostly when I see people use it, there are better ways to do what they want, though I accept that sometimes there aren't; for those occasions the API should suffice.

That's great, good to know. Thanks Rory.
 
Upvote 0
Try using:

MacScript ("tell application " & Chr(34) & "System Events" & Chr(34) & " to key code 7") 'this will type the letter "x"

' I use this for my validation dropdown lists to automate the dropdown action when the cell is clicked:
MacScript ("tell application " & Chr(34) & "System Events" & Chr(34) & " to key code 126 using option down")

'You can also use the "Control", "Command" & "Option" Keys:
'tell application "System Events" to keystroke "f" using {control down, command down, option down}

'Key Code numbers are as follows:
'0 = a
'1 = s
'2 = d
'3 = f
'4 = h
'5 = g
'6 = z
'7 = x
'8 = c
'9 = v
'10 = ISO_Section
'11 = b
'12 = q
'13 = w
'14 = e
'15 = r
'16 = y
'17 = t
'18 = 1
'19 = 2
'20 = 3
'21 = 4
'22 = 6
'23 = 5
'24 = (Equal)
'25 = 9
'26 = 7
'27 = (Minus)
'28 = 8
'29 = 0 (number 0)
'30 = (RightBracket)
'31 = o (letter "o")
'32 = u
'33 = (LeftBracket)
'34 = i (Letter "eye")
'35 = p
'36 = (Return)
'37 = l (Letter "el")
'38 = j
'39 = (Quote)
'40 = k
'41 = (Semicolon)
'42 = (Backslash)
'43 = (Comma)
'44 = (Slash)
'45 = n
'46 = m
'47 = (Period)
'48 = (Tab)
'49 = (Space)
'50 = ANSI_Grave
'51 = (Delete)
'53 = (Escape)
'55 = (Command)
'56 = (Shift)
'57 = (CapsLock)
'58 = (Option)
'59 0x3B Control
'60 0x3C RightShift
'61 0x3D RightOption
'62 0x3E RightControl
'63 0x3F Function
'64 0x40 F17
'65 0x41 ANSI_KeypadDecimal
'67 0x43 ANSI_KeypadMultiply
'69 0x45 ANSI_KeypadPlus
'71 0x47 ANSI_KeypadClear
'72 0x48 VolumeUp
'73 0x49 VolumeDown
'74 0x4A Mute
'75 0x4B ANSI_KeypadDivide
'76 0x4C ANSI_KeypadEnter
'78 0x4E ANSI_KeypadMinus
'79 0x4F F18
'80 0x50 F19
'81 0x51 ANSI_KeypadEquals
'82 0x52 ANSI_Keypad0
'83 0x53 ANSI_Keypad1
'84 0x54 ANSI_Keypad2
'85 0x55 ANSI_Keypad3
'86 0x56 ANSI_Keypad4
'87 0x57 ANSI_Keypad5
'88 0x58 ANSI_Keypad6
'89 0x59 ANSI_Keypad7
'90 0x5A F20
'91 0x5B ANSI_Keypad8
'92 0x5C ANSI_Keypad9
'93 0x5D JIS_Yen
'94 0x5E JIS_Underscore
'95 0x5F JIS_KeypadComma
'96 0x60 F5
'97 0x61 F6
'98 0x62 F7
'99 0x63 F3
'100 0x64 F8
'101 0x65 F9
''102 0x66 JIS_Eisu
'103 0x67 F11
'104 0x68 JIS_Kana
'105 0x69 F13
'106 0x6A F16
'107 0x6B F14
''109 0x6D F10
'111 0x6F F12
'113 0x71 F15
'114 0x72 Help
'115 0x73 Home
'116 0x74 PageUp
'117 0x75 ForwardDelete
'118 0x76 F4
'119 0x77 End
'120 0x78 F2
'121 0x79 PageDown
'122 0x7A F1
'123 0x7B LeftArrow
'124 0x7C RightArrow
'125 0x7D DownArrow
'126 0x7E UpArrow
 
Upvote 0

Forum statistics

Threads
1,215,153
Messages
6,123,325
Members
449,097
Latest member
gameover8

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