• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

Closing a window based on caption

I need a clean way of closing a window based on the caption of the window.
For instance, if I want to close a window that has the word "DELETE" in it, how do I set that up?
1 Solution
Dim lHandle As Long
Dim lRez As Long

         ' Find the Menu window id.
      lHandle = FindWindow(vbNullString, "*Microsoft Outlook*")
         ' Kill it once
      If lHandle Then
         lRez = SendMessage(lHandle, WM_CLOSE, 0, 0)
         ' Kill it again in case didnt close
         lRez = SendMessage(lHandle, WM_DESTROY, 0, 0)
      End If 'lHandle
   Loop While lHandle <> 0

Change the Microsoft Outlook with what you need in there. This code is based on VB6 SP6
You may need:

Private Const WM_DESTROY = &H02
Private Const WM_CLOSE = &H10
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now