Option Explicit
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Declare Function GetDesktopWindow Lib "user32" () As Long
Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdSHow As Long) As Long
Const WM_LBUTTONDOWN = &H201
Const WM_LBUTTONUP = &H202
Public CancelMe As Boolean
Sub Upate_EndNote_References()
Dim parent_handle&, child_handle&, ClassName$, SearchName$
' Don't stop till the user clicks on the stop button
CancelBtn.Show vbModeless
CancelMe = False
' Window information
ClassName = "#32770"
SearchName = "Review Available Updates"
' Click on the update all fields buttons and save the changes
DoEvents
Do While CancelMe = False
' Find the correct window
parent_handle = FindWindowLike(GetDesktopWindow(), SearchName, ClassName)
DoEvents: If CancelMe = True Then Exit Do
If parent_handle <> 0 Then
' Click the Update All Fields button
child_handle = FindWindowEx(parent_handle, 0, "Button", "Update All Fields ->")
Click_Button (child_handle)
' Click the Save Updates button
child_handle = FindWindowEx(parent_handle, 0, "Button", "Save Updates")
Click_Button (child_handle)
End If
Loop
Unload CancelBtn
MsgBox "Task completed...", vbInformation
End Sub
Private Sub Click_Button(child_handle As Long)
DoEvents: If CancelMe = True Then Exit Sub
Call PostMessage(child_handle, WM_LBUTTONDOWN, 0, 0)
Call PostMessage(child_handle, WM_LBUTTONUP, 0, 0)
End Sub
Function FindWindowLike(hWndParent As Long, Caption As String, ClassName As String) As Long
Dim hWnd&
Const GW_HWNDNEXT = 2, GW_CHILD = 5
DoEvents: If CancelMe = True Then Exit Function
' Find window using a like function
hWnd = GetWindow(hWndParent, GW_CHILD)
Do Until hWnd = 0
If WindowText(hWnd) Like "*" & Caption & "*" Then
FindWindowLike = hWnd
Exit Do
End If
hWnd = GetWindow(hWnd, GW_HWNDNEXT)
Loop
End Function
Function WindowText(hWnd As Long) As String
Dim lng&, str$
Const WM_GETTEXT = &HD, WM_GETTEXTLENGTH = &HE
DoEvents: If CancelMe = True Then Exit Function
' Convert ID to text
If hWnd <> 0 Then
lng = SendMessage(hWnd, WM_GETTEXTLENGTH, 0&, 0&) + 1
If lng > 0 Then
str = String$(lng, vbNullChar)
lng = SendMessage(hWnd, WM_GETTEXT, lng, ByVal str)
If lng > 0 Then WindowText = Left$(str, lng)
End If
End If
End Function
Function apicShowWindow(strClassName As String, strWindowName As String, lngState As Long)
Dim lngWnd&, intRet%
DoEvents: If CancelMe = True Then Exit Function
' Use Findwindow and ShowWindow together to manipulate window
lngWnd = FindWindow(strClassName, strWindowName)
apicShowWindow = ShowWindow(lngWnd, lngState)
End Function
Bookmarks