WD: Using SendKeys with Dialog Boxes in WordBasic

Last reviewed: February 2, 1998
Article ID: Q52159
The information in this article applies to:
  • Microsoft Word for Windows, versions 1.0, 1.1, 1.1a, 2.0, 2.0a, 2.0a-CD, 2.0b, 2.0c, 6.0, 6.0a, 6.0c
  • Microsoft Word for Windows 95, versions 7.0, 7.0a
  • Word for the Macintosh, versions 6.0, 6.0.1

SYMPTOMS

When you use the SendKeys function after calling a dialog box, your macro does not run as expected. For example, the following macro does not run correctly:

   Sub MAIN
      ChDir "C:\winword"           ' Change Directory
      FileOpen "test.doc"          ' File Open dialog box.
      SendKeys "{Enter}", -1       ' Send the Enter key.
   End Sub

CAUSE

WordBasic code is processed one line at a time. The SendKeys function sends the specified keys to the active application, just as if they were typed at the keyboard. If Word is not the active program and Wait is set to -1, Word will wait for all the keys to be processed before proceeding.

Whenever a Word dialog box is opened, it becomes the active foreground application and is ready to take commands; it also must be terminated (with OK or Cancel) before the next line can be processed.

WORKAROUND

In the example, the SendKey "{Enter}" command should come before the FileOpen command so that the File dialog box can process the keys and close. Then, the next line of code can be processed.

   Sub MAIN
      ChDir "C:\winword"
      SendKeys "{Enter}", -1       ' Send the Enter Key.
      FileOpen "test.doc"          ' File Open Dialog box.
   End Sub

Word waits for the dialog box to open, and then it sends the ENTER key to the FileOpen dialog box.


Additional query words: window
Keywords : macword ntword winword winword2 word6 word7 word95 wordnt kbmacro
Version : WINDOWS:1.0,1.1,1.1a,2.0,2.0a,2.0a- CD,2.0b,2.0c,6.0,6.0a,6.0c,7.0,7.0a; MACINTOSH:6.0,6.0.1,6.0.1a
Platform : MACINTOSH Win95 WINDOWS winnt
Issue type : kbprb
Solution Type : kbworkaround


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