SendKeys Statement Doesn't Send Keystrokes to Module in MS Excel

Last reviewed: July 29, 1997
Article ID: Q126098
The information in this article applies to:
  • Microsoft Visual Basic Programming System, Applications Edition, version 1.0
  • Microsoft Excel for Windows, versions 5.0, 5.0c

SYMPTOMS

You cannot use the SendKeys statement to send keystrokes to a module window in Microsoft Excel. For example, if you run the following procedure while a module sheet is active in Microsoft Excel, Microsoft Excel is activated, but text is not entered in the module:

   Sub To_Excel()
      AppActivate "Microsoft Excel"
      SendKeys "Sub Test"
   End Sub

Note that the SendKeys command sends the keystrokes as expected if a worksheet is active in Microsoft Excel when you run the above procedure.

WORKAROUND

To work around this behavior, for example if you want to use a macro in Microsoft Project to enter code in a Visual Basic module in Microsoft Excel, you can write the desired code to a text file, and then use the Module.InsertFile method to insert the code in a Module in Microsoft Excel. The following is an example of using this method:

   Sub ToExcel()
      ' Dimension xl variable as Object type
      Dim xl As Object
      ' Set variable xl equal to the Excel Application object
      Set xl = Excel.Application

      With xl
         ' Open workbook in Excel
         ' Substitute your file name in the following line
         .Workbooks.Open ("c:\excel\files\test.xls")
         ' Insert text file that contains code
         ' Substitute your text file name in the following line
         .ActiveWorkbook.Modules("Module1").InsertFile "c:\code.txt"
         ' Close file, saving changes
         .ActiveWorkbook.Close True
         ' Quit application Excel
         .Quit
      End With

    End Sub

Microsoft provides examples of Visual Basic procedures for illustration only, without warranty either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This Visual Basic procedure is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this procedure to suit customer requirements for a particular purpose.

MORE INFORMATION

Note that if you run the following procedure while the Module Editor view is active in Microsoft Project, Microsoft Project is activated, and the text "Sub Test" is entered in the active module:

   Sub Test()
      AppActivate "Microsoft Project"
      SendKeys "Sub Test"
   End Sub

REFERENCES

For more information about the SendKeys Statement or the SendKeys Method, choose the Search button in the Visual Basic Reference and type:

    SendKeys
Keywords          : kbcode kbprg
Version           : 1.00
Platform          : WINDOWS


================================================================================


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: July 29, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.