Visual Basic Code of the Week (COTW)
Issue #101
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 1999 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

This week's topic: Finding a window with only a partial caption

Six months of VB Training for only $49.99

Want to get up to speed on the latest Visual Basic programming? Includes Visual Basic 6 and Visual InterDev 6. Check out our training programs at

Get paid to surf the web!

If you would like to get paid for surfing the web, jump to


In this Issue

This issue introduces a module which shows how retrieve a window by its caption. Normally this is a simple API call. The biggest problem with the standard FindWindow API call is that the caption of the window passed to the routine much match exactly. Our routine uses the built-in pattern matching of Visual Basic to provide a very flexible way to find a window which is currently open.

A good use of this routine is a simple routine to wait for a particular window to show up after running a Shell command.

If you have any questions about using this module, let us know at


Public Function WindowCaption(ByVal hWnd As Long) As String

Returns a window caption by its handle. This routine strips out any nulls the API puts at the end of the string.

Public Function FindAnyTopWindow(sCaption As String) As Long

This routine does all the work. It will search through all the child windows of the desktop window to find a window which has the caption specified by sCaption. You can pass strings which contain the Visual Basic pattern matching characters. For example, "Microsoft Word*" will find the first open instance of Word regardless of what file you happen to have open in Word.

A zero will be returned if there were no matches.


See above descriptions.

Sample Usage

This sample assumes you have a form with a text box control called txtCaption and two label controls called lblCaption and lblHwnd. The sample will take the input from txtCaption and search for that window. It will set lblCaption with the full caption it found and lblHwnd with the handle to the window it found.

    ' This code would probably appear within a button or a background routine.
    Dim hWnd As Long
    hWnd = FindAnyTopWindow(txtCaption.Text)
    If hWnd = 0 Then
        lblCaption = ""
        lblHwnd = 0
        lblCaption = WindowCaption(hWnd)
        lblHwnd = hWnd
    End If

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945