What is the difference between SendKeys wait argument false or true in VBA?

SendKeys is a VBA method that allows you to send one or more keystrokes to the active window as if typed at the keyboard. The wait argument is an optional boolean value that specifies the wait mode. If False (default), control is returned to the procedure immediately after the keys are sent. If True, keystrokes must be processed before control is returned to the procedure.

The basic theory behind the wait argument is that it determines whether SendKeys operates synchronously or asynchronously. Synchronous means that the code waits for the operation to complete before moving on to the next line. Asynchronous means that the code continues to run without waiting for the operation to finish. Depending on what you have sent and what your automation expectations are, it may be helpful to use either mode. For example, if you have sent a command to copy or paste, you would want the operation to complete before the next line of code executes. In that case, you would use wait=True. Otherwise, a subsequent paste command might paste the wrong content or nothing at all if your code executes faster than the application you are automating can keep up with.

Procedures

To use SendKeys, you need to follow these steps:

  • Declare a string variable that contains the keystrokes you want to send. You can use special characters and codes to represent keys that are not displayed when you press them, such as ENTER or TAB. For a full list of codes, see this link.
  • Call the SendKeys method with the string variable and the wait argument. For example, SendKeys str, True.
  • Optionally, you can use the Shell function to run another application and then send keystrokes to it. For example, Shell “C:\Windows\System32\calc.exe”, vbNormalFocus.

Example

Suppose you want to use SendKeys to automate the following task:

  • Open the Calculator application.
  • Enter the numbers 1, 2, and 3, separated by plus signs.
  • Press the equal sign to get the result.
  • Close the Calculator application.

Here is the code you can use:

Sub SendKeysExample()
    Dim str As String
    'Set up the string variable with the keystrokes
    str = "1{+}2{+}3{=}~%{F4}"
    'Run the Calculator application
    Shell "C:\Windows\System32\calc.exe", vbNormalFocus
    'Send the keystrokes with wait mode True
    SendKeys str, True
End Sub

Explanation

  • The string variable str contains the keystrokes 1, 2, and 3, followed by the plus sign {+}, the equal sign {=}, the tilde ~ (which represents the ENTER key), the percent sign % (which represents the ALT key), and the F4 key {F4}.
  • The Shell function runs the Calculator application and sets the focus to it.
  • The SendKeys method sends the keystrokes to the Calculator application and waits for them to be processed before returning control to the procedure. This ensures that the calculation is completed and the result is displayed before the application is closed.
  • The result of the calculation is 6, which is shown in the Calculator window.
  • The keystroke combination %{F4} sends the ALT+F4 command, which closes the Calculator application.

Other approaches

SendKeys is not a reliable or recommended way to automate tasks in Excel or other applications. It has several limitations and drawbacks, such as:

  • It can’t send keystrokes to an application that is not designed to run in Windows or Macintosh.
  • It can’t send the PRINT SCREEN key {PRTSC} to any application.
  • It may cause unexpected results or errors if the active window changes or the application is not ready to receive the keystrokes.
  • It may interfere with the user’s input or other programs that are running.

Therefore, it is better to use other approaches that are more robust and secure, such as:

  • The Application object, which provides methods and properties to control and interact with Excel and other Office applications. For example, you can use the Application.Run method to run a macro or a function in another workbook or add-in.
  • The Workbook object, which represents a workbook in Excel and allows you to manipulate its contents and settings. For example, you can use the Workbook.SaveAs method to save a copy of the workbook with a different name or format.
  • The Worksheet object, which represents a worksheet in Excel and allows you to access and modify its cells, ranges, tables, charts, and other elements. For example, you can use the Worksheet.Range property to return a range object that represents a cell or a group of cells on the worksheet.
  • The Range object, which represents a cell or a group of cells on a worksheet and allows you to perform various operations on them, such as entering values, formulas, formats, or comments, copying, cutting, pasting, sorting, filtering, or validating data, and applying conditional formatting or data bars. For example, you can use the Range.Value property to get or set the value of a cell or a range of cells.
  • The WorksheetFunction object, which provides access to many of the functions that are available in Excel, such as mathematical, statistical, financial, logical, text, date and time, lookup and reference, and information functions. For example, you can use the WorksheetFunction.Sum function to return the sum of a range of cells or values.

For more information and examples on how to use these and other objects, methods, properties, and functions in VBA, please visit this link.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *