Wrote this code to hit the button if a message box "Non perpendicular UCS" turns up. Maybe you coud modify it for your needs.
P.S. Its VB6 Code not VBA
Put this in a module
Option Explicit
Private Declare Function FindWindow Lib "User32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetClassName Lib "User32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
Public Declare Function ShowWindow Lib "User32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function GetParent Lib "User32" (ByVal hwnd As Long) As Long
Public Sub CheckForTargetAutoCADWindow()
Dim hwnd As Long
Dim lpClassName As String
Dim nMaxCount As Long
Dim lresult As Long
Dim delayCount As Long
'Look to see if the target window is---------------------
'on the desktop '
nMaxCount = 256 '
lpClassName = Space(nMaxCount) '
hwnd = FindWindow(vbNullString, "AutoCAD Message") '
'<
lresult = GetClassName(hwnd, lpClassName, nMaxCount) '
hwnd = FindWindow(lpClassName, vbNullString) '
'--------------------------------------------------------
If hwnd <> 0 Then
'if target window found ------------------------------
'Update form caption '
frmSpy.Caption = "ACAD UCS (Hit Enter) Found" '
'Wait a short while for AutoCAD screen to '
'complete loading '
For delayCount = 1 To 10000 '
DoEvents '
Next '
'Focus to the AutoCAD parent. Sending enter to the '
'actual AutoCAD message does not seem to be reliable '
Call ShowWindow(GetParent(hwnd), 5) '
'Wait a shorter while '
For delayCount = 1 To 1000 '
DoEvents '
Next '
'Send enter to clear the message '
SendKeys ("~") '
'Wait a short while for the hit to register '
'to ensure that we do not double hit '
For delayCount = 1 To 10000 '
DoEvents '
Next '
'End if target window found --------------------------
Else
'Update caption if target window not visible--------------
If frmSpy.Caption <> "ACAD UCS (Hit Enter) Not Found" _
Then frmSpy.Caption = _
"ACAD UCS (Hit Enter) Not Found"
'---------------------------------------------------------
End If
End Sub
Add a timer to the form, call the form frmSpy (name property) and add the code
Private Sub Form_Load()
'Make the form title visible but to bottom ---------------
'right of screen '
Me.Height = 0 '
Me.Top = Screen.Height - Me.Height '
Me.Left = Screen.Width - Me.Width '
'---------------------------------------------------------
'Initiate Timer ------------------------------------------
Timer1.Interval = 100 '
Timer1.Enabled = True '
'---------------------------------------------------------
End Sub
Private Sub Timer1_Timer()
'Stop Timer----------------------------------------------
Timer1.Enabled = False '
'--------------------------------------------------------
'Look for a target window to sen a click to--------------
CheckForTargetAutoCADWindow '
'--------------------------------------------------------
'Restart Timer-------------------------------------------
Timer1.Enabled = True '
'--------------------------------------------------------
End Sub
'You may need to mess around with the delay code.