Interaction.AppActivate Method (Int32)
Activates an application that is already running.
Assembly: Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)
'Declaration <SecurityPermissionAttribute(SecurityAction.Demand, Flags := SecurityPermissionFlag.UnmanagedCode)> _ Public Shared Sub AppActivate ( _ ProcessId As Integer _ )
You use AppActivate to bring an application's active window into focus. You might not have a handle or a reference to the active window, or even know which window is active at a given moment. In such a case, you cannot use the Focus method.
The AppActivate function changes the focus to the named application or window but does not affect whether it is maximized or minimized. Focus moves away from the activated application window when the user takes some action to change the focus or close the window. You can use the Shell function to start an application and set the window style.
If you use the Title parameter, AppActivate uses a case-insensitive comparison but otherwise requires an exact match with the contents of the title bar. It first looks through the top-level windows and then through the child windows. If it cannot find a match, it throws an ArgumentException.
You can use AppActivate only with processes that own windows. Most console applications do not own windows, which means that they do not appear in the list of processes that AppActivate searches. When running from a console application, the system creates a separate process to run the application and returns the output to the console process. Consequently, when you request the current process ID, you get the process ID of this separate process, rather than the console application's process ID.
At run time, the AppActivate function activates any running application with a title that matches Title or with a process ID that matches ProcessId. If there is no exact match, it activates any application whose title string ends with Title. If there is more than one application named Title, the AppActivate function arbitrarily chooses one to activate.
This example illustrates various uses of the AppActivate function to activate an application window. If a Notepad process is not running, the example throws an ArgumentException. The Shell procedure assumes the applications are in the paths specified.
Dim notepadID As Integer ' Activate a running Notepad process. AppActivate("Untitled - Notepad") ' AppActivate can also use the return value of the Shell function. ' Shell runs a new instance of Notepad. notepadID = Shell("C:\WINNT\NOTEPAD.EXE", AppWinStyle.NormalFocus) ' Activate the new instance of Notepad. AppActivate(notepadID)