Web development India freelance website designer developer India SEO

As said by the author, "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."
To try this example, you will need to follow the steps indicated below. This example requires one form consisting of only one command button, and a BAS module to hold the declarations and one SUB.
Start Visual Basic and add a new module to the project. Inside this module, insert the constants and declarations shown below:

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&)
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.
Click Buttons in Other Programs using the API

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

India web developer web development India | Freelance web development ecommerce web developer | Prayagasoft - web designer India, Ecommerce developer india, Ecommerce design