'*************************************************************************** 'Windows API/Global Declarations for :Click Buttons in Other Programs using the API '*************************************************************************** Global Const WM_COMMAND = &H111 Global Const GW_Child = 5 Global Const GW_HWNDFIRST = 0 Global Const GW_HWNDLAST = 1 Global Const GW_HWNDNEXT = 2 Global Const GW_HWNDPREV = 3 Global Const GW_OWNER = 4 Declare Function GetDesktopWindow Lib "User" () As Integer Declare Function GetParent Lib "User" (ByVal hWnd As Integer) As Integer Declare Function GetWindow Lib "user" (ByVal hWnd%, ByVal wCmd%) As Integer Declare Function GetWindowText Lib "User" (ByVal hWnd%, ByVal lpString$, ByVal nMaxCount%) As Integer Declare Function GetWindowTextLength Lib "User" (ByVal hWnd As Integer) As Integer Declare Function GetWindowWord Lib "User" (ByVal hWnd As Integer, ByVal nIndex As Integer) As Integer Declare Function SendMessageByNum& Lib "User" Alias "SendMessage" (ByVal hWnd%, ByVal wMsg%, ByVal wparam%, ByVal lparam&) '******************************** '*************************************************************************** ' Name: Click Buttons in Other Programs using the API ' Description:This function is a simple yet a very useful one. It ' will click any button on any form or it will run a menu command ' By: Phoenix ' ' Inputs:None ' Returns:None ' Assumes:None ' Side Effects:None ' 'Code provided by Planet Source Code(tm) 'as is', without ' warranties as to performance, fitness, merchantability, ' and any other warranty (whether expressed or implied). '*************************************************************************** It is now necessary to add a SUB to the module as indicated below. This is the SUB that actually clicks another form's buttons. Sub Click (Btnhwnd As Integer) Dim ChildID As Integer Dim Nul As Integer Const GWW_ID = (-12) ChildID = GetWindowWord(Btnhwnd, GWW_ID) Nul = SendMessageByNum(GetParent(Btnhwnd), WM_COMMAND, ChildID, ByVal CLng(Btnhwnd)) End Sub You now have the entire routine necessary to click a button on another form. However, you need to be able to tell the SUB which button to click. Therefore, we have added the routine below to locate a button that you all have and click it. Make sure your Visual Basic® "Project" window is visible. This routine is only used to locate a button and should be modified to find other command buttons and windows. The last step is to place a command button on your form. You may want to place the command button in upper right hand corner of the window and make the window just large enough to hold the button. The procedure will attempt to locate the "View Code" button in the Project Window. If it can find it, it will click it for you, causing the code window for the form to appear. Place the following code in the command button's CLICK event. Sub Command1_Click () On Error Resume Next Dim Btn As Integer, CurHwnd As Integer, T As String Dim Length As Integer, x As Integer, y As Integer ' ' Trying to find the handle of the View Code Button so that ' ' by clicking this program's button, we can see the code ' ' window for this form. CurHwnd = GetDesktopWindow()'Get Desktop handle CurHwnd = GetWindow(CurHwnd, GW_Child) 'Find Child Windows of Desktop Do If CurHwnd = 0 Then Exit Do 'No (more) matches found ' ' Find out how long the text in this window is Length = GetWindowTextLength(CurHwnd) T = Space$(Length + 1)'Allocate buffer space Length = GetWindowText(CurHwnd, T, Length + 1) If InStr(UCase$(T), "PROJECT") Then ' ' The word "Project" was found in this Window's text ' ' so this is likely VB's "Project" window CurHwnd = GetWindow(CurHwnd, GW_Child) ' ' Looking now for the Project Window's child windows Do If CurHwnd = 0 Then Exit Sub 'No (more) matches found ' ' Find out how long the text in this window is Length = GetWindowTextLength(CurHwnd) T = Space$(Length + 1)'Allocate buffer space Length = GetWindowText(CurHwnd, T, Length + 1) If InStr(UCase$(T), "VIEW CODE") Then ' ' This is the handle we want Click CurHwnd'Click the View Code Button Exit Sub 'Exit the Sub End If CurHwnd = GetWindow(CurHwnd, GW_HwndNext) 'Keep looking Loop End If CurHwnd = GetWindow(CurHwnd, GW_HwndNext) 'Keep looking Loop End Sub Close ALL of your code windows before running this program. If you do not, you might not notice that it was successful. The final step is to run the program. Keep in mind that the sole objective of this event procedure is to locate the "View Code" Button in the Project window and pass its handle to the CLICK SUB in the module so that it can actually do the clicking for you.