Microsoft KB Archive/87773

= PRB: SendKeys May Return Illegal Function Call Error =

Article ID: 87773

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q87773



SYMPTOMS
The SendKeys statement reports the error &quot;Illegal function call&quot; when its argument contains an incorrectly formatted string. This article describes specific circumstances that cause this error, and contains a code example that shows how to send any string with SendKeys.



CAUSE
The following characters have special meaning to the SendKeys statement:      + ^ % ~  [ ] { } The SendKeys statement reports an &quot;Illegal function call&quot; error if its argument contains one of the following, not enclosed in braces:


 * An unmatched parenthesis or bracket {}
 * A bracket []
 * Braces containing an undefined character sequence, such as {abc}



RESOLUTION
To prevent the SendKeys statement from interpreting a character, enclose the character in braces {}. For example, to send the string

The interest rate is 5% (annually).

Use the following SendKeys syntax: SendKeys &quot;The interest rate is 5{%} {(}annually{)}.&quot;



Step-by-Step Example
The following example demonstrates how to use the SendKeys statement to send strings that would normally cause an &quot;Illegal function call&quot; error:

 Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default. Place a text box (Text1) on Form1. Place a command button (Command1) on Form1.  Enter the following code: Sub Command1_Click Text1.SetFocus SendKeys sendkeys_prepare(&quot;1+2^5% &quot;) SendKeys sendkeys_prepare(&quot;[] &quot;) SendKeys sendkeys_prepare(&quot;{abc}&quot;) End Sub

' The following function puts braces {} around characters that ' are special to the SendKeys statement. Function sendkeys_prepare (in As String) As String For i% = 1 To Len(in) ' Get the next character into c$. c$ = Mid$(in, i%, 1) ' If c$ is one of the special characters. If InStr(&quot;+^%~[]{}&quot;, c$) Then out$ = out$ + &quot;{&quot; + c$ + &quot;}&quot; Else out$ = out$ + c$        End If      Next sendkeys_prepare = out$ End Function  Press F5 to run the program. Click Command1. Some example text containing characters special to SendKeys appear in Text1.</li></ol>

<div class="references_section">