Skip to main content

AppActivate statement

Table of contents
  1. Syntax
  2. Remarks
  3. Example

Activates an application window.

Syntax

AppActivate title, [ wait ]

The AppActivate statement syntax has these named arguments:

Part Description
title Required. String expression specifying the title in the title bar of the application window you want to activate. The task ID returned by the Shell function can be used in place of title to activate an application.
wait Optional. Boolean value specifying whether the calling application has the focus before activating another. If False (default), the specified application is immediately activated, even if the calling application does not have the focus. If True, the calling application waits until it has the focus, and then activates the specified application.

Remarks

The AppActivate statement changes the focus to the named application or window but does not affect whether it is maximized or minimized. Focus moves from the activated application window when the user takes some action to change the focus or close the window. Use the Shell function to start an application and set the window style.

In determining which application to activate, title is compared to the title string of each running application. If there is no exact match, any application whose title string begins with title is activated. If there is more than one instance of the application named by title, one instance is arbitrarily activated.

Example

This example illustrates various uses of the AppActivate statement to activate an application window. The Shell statements assume that the applications are in the paths specified. On the Macintosh, the default drive name is "HD" and portions of the pathname are separated by colons instead of backslashes.

Dim MyAppID, ReturnValue
AppActivate "Microsoft Word" ' Activate Microsoft Word.
 
' AppActivate can also use the return value of the Shell function.
MyAppID = Shell("C:\WORD\WINWORD.EXE", 1) ' Run Microsoft Word.
AppActivate MyAppID ' Activate Microsoft Word.
 
' You can also use the return value of the Shell function.
ReturnValue = Shell("c:\EXCEL\EXCEL.EXE", 1) ' Run Microsoft Excel.
AppActivate ReturnValue ' Activate Microsoft Excel.

Leave a comment

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

Format your code: <pre><code class="language-vba">place your code here</code></pre>

2 comments
  1. DV
    Dominic Vella

    It Doesn't work.
    Public Sub UseCalculator()
    Dim ReturnValue As Double
    Dim I

    ReturnValue = Shell("CALC.EXE", 1) ' Run Calculator.
    DoEvents: Application.Wait Now() + TimeSerial(0, 0, 1): DoEvents

    '===============
    AppActivate ReturnValue ' Activate the Calculator."
    ' Error Here: Run-time error '5'. Invalid procedure call or argument
    ' The fix is to use the title (very dangerous), not the ID (ID is preferred)
    AppActivate "Calculator" ' Activate the Calculator."
    '===============
    DoEvents: Application.Wait Now() + 0.0000005: DoEvents

    For I = 1 To 5 ' Set up counting loop.
    SendKeys "{+}", True ' Send keystrokes to Calculator
    DoEvents: Application.Wait Now() + 0.00001: DoEvents
    SendKeys I, True ' Send keystrokes to Calculator
    DoEvents: Application.Wait Now() + 0.00001: DoEvents
    Next I ' to add each value of I.
    SendKeys "=", True ' Get grand total.
    DoEvents: Application.Wait Now() + TimeSerial(0, 0, 5): DoEvents
    SendKeys "%{F4}", True ' Send ALT+F4 to close Calculator.
    End Sub

    • EX

      @Dominic Vella I Don't know why AppActivate can't activate Calc's task ID, but activating notpad.exe has no problem.

      Sub UseCalculator()
          Dim AppFile As String
          Dim CalcTaskID As Double
          
          AppFile = "Calc.exe"
          On Error Resume Next
          AppActivate "Calculator"
          If Err <> 0 Then
              Err = 0
              CalcTaskID = Shell(AppFile, 1)
              If Err <> 0 Then MsgBox "Can not start Calculator"
          End If
          
          DoEvents: Application.Wait Now() + TimeSerial(0, 0, 5): DoEvents
          For I = 1 To 5 ' Set up counting loop.
              SendKeys "{+}", True ' Send keystrokes to Calculator
              DoEvents: Application.Wait Now() + 0.00001: DoEvents
              SendKeys I, True ' Send keystrokes to Calculator
              DoEvents: Application.Wait Now() + 0.00001: DoEvents
          Next I ' to add each value of I.
          
          SendKeys "=", True ' Get grand total.
          DoEvents: Application.Wait Now() + TimeSerial(0, 0, 5): DoEvents
          SendKeys "%{F4}", True ' Send ALT+F4 to close Calculator.
      End Sub