PRB: SendKeys May Return Illegal Function Call Error

Last reviewed: June 21, 1995
Article ID: Q87773
The information in this article applies to:
  • Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0
  • Microsoft Visual Basic programming system for Windows, version 1.0

SUMMARY

SYMPTOMS

   The SendKeys statement reports the error "Illegal function call" 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 "Illegal function call" 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 "The interest rate is 5{%} {(}annually{)}."

MORE INFORMATION

Step-by-Step Example

The following example demonstrates how to use the SendKeys statement to send strings that would normally cause an "Illegal function call" error:

  1. 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.

  2. Place a text box (Text1) on Form1.

  3. Place a command button (Command1) on Form1.

  4. Enter the following code:

       Sub Command1_Click ()
          Text1.SetFocus
          SendKeys sendkeys_prepare("1+2^5% ")
          SendKeys sendkeys_prepare("[] ")
          SendKeys sendkeys_prepare("{abc}")
       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("+^%~()[]{}", c$) Then
                out$ = out$ + "{" + c$ + "}"
             Else
                out$ = out$ + c$
             End If
          Next
          sendkeys_prepare = out$
       End Function
    
    

  5. Press F5 to run the program. Click Command1. Some example text
containing
   characters special to SendKeys appear in Text1.

REFERENCES

"Microsoft Visual Basic: Language Reference," version 1.0, pages 283-284


Additional reference words: 1.00 2.00 3.00
KBCategory: kbprg kbcode kbprb
KBSubcategory: PrgCtrlsStd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.