AppActivate Procedure

   
AppActivate Procedure

Class

Microsoft.VisualBasic.Interaction

Syntax

 [Interaction.]AppActivate(   title   ) 
title (required; String or Integer)

The name of the application as currently shown in the application window title bar. This can also be the task ID returned from the Shell function.

Description

Activates a window based on its caption

Rules at a Glance

  • AppActivate performs a case-insensitive search on all top-level windows for a window caption that matches title . If an exact match is found, the window is activated. If no match is found, then the window captions are searched for a prefix match ( title matches the beginning of the window caption). For example, the title "Microsoft Word" matches "Microsoft Word - MyDocument.doc" . If a prefix match is found, the window is activated. Note that if multiple prefix matches are found, there is no way to predict which matching window will be activated.

  • The window state (Maximized, Minimized, or Normal) of the activated application is not affected by AppActivate .

  • If a matching application cannot be found, an exception of type System.ArgumentException is raised, and runtime error 5, "Invalid procedure call or argument," is generated.

Example

 Private Sub Button2_Click(ByVal sender As System.Object, _                           ByVal e As System.EventArgs) _                           Handles Button2.Click     Dim bVoid As Boolean     bVoid = ActivateAnApp("Microsoft Excel")      End Sub Function ActivateAnApp(vAppTitle As String) As Boolean          On Error GoTo Activate_Err          ActivateAnApp = False     AppActivate(vAppTitle)     ActivateAnApp = True          Exit Function      Activate_Err:     MsgBox ("Application " & vAppTitle & _            " could not be activated")      End Function 

Programming Tips and Gotchas

  • AppActivate searches only top-level windows.

  • You can also use the task ID returned by the Shell function with the AppActivate statement, as this simple example demonstrates :

     Option Explicit Private vAppID Private Sub Button1_Click(ByVal sender As System.Object, _                           ByVal e As System.EventArgs) _                           Handles Button1.Click     vAppID = Shell("C:\Program Files\Internet Explorer\IEXPLORE.EXE") End Sub Private Sub Button2_Click(ByVal sender As System.Object, _                           ByVal e As System.EventArgs) _                           Handles Button2.Click     AppActivate vAppID End Sub 
  • AppActivate is very difficult to use with applications whose application titles change to reflect the state or context of the application. Microsoft Outlook illustrates an excellent example of this problem. If the user has Outlook in the Calendar section, the title bar reads "Calendar - Microsoft Outlook," whereas if in the Inbox section, the title bar reads "Inbox - Microsoft Outlook." In situations such as this, we must resort to other techniques, such as using Win32 API methods , to enumerate all windows and check the captions directly.

  • AppActivate is often used to give the focus to a particular window before keystrokes are sent to it using the SendKeys statement, which sends keystrokes to the active window only.

VB.NET/VB 6 Differences

In VB 6, AppActivate has a second optional parameter, wait , a Boolean that determines whether the application calling AppActivate must have the focus for the window indicated by title to be activated. In VB.NET, wait is not supported.

See Also

Shell Function

   


VB.Net Language in a Nutshell
VB.NET Language in a Nutshell
ISBN: B00006L54Q
EAN: N/A
Year: 2002
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net