Here is the solution I arrived at, which is working exactly as intended. Cannot claim credit for it as @Jimi gave me all the necessary pointers for me to arrive at this. However, the snippet may be helpful for others trying to navigate UI Automation for the first time, like I was.
Private Sub StartListening()
Try
Dim eventHandler As AutomationEventHandler = AddressOf OnWindowOpenOrClose
Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Subtree, eventHandler)
Catch ex As Exception
End Try
End Sub
Private Sub StopListening()
Try
Automation.RemoveAllEventHandlers()
Catch ex As Exception
End Try
End Sub
Private Sub OnWindowOpenOrClose(ByVal src As Object, ByVal e As AutomationEventArgs)
Dim sourceElement As AutomationElement
Dim okButton As AutomationElement
Dim okButtonCondition As Condition
Dim okButtonInvokePattern As InvokePattern
Try
sourceElement = DirectCast(src, AutomationElement)
If sourceElement IsNot Nothing Then
If sourceElement.Current.ControlType.Equals(ControlType.Window) AndAlso sourceElement.Current.ClassName = "NUIDialog" AndAlso sourceElement.Current.Name = "<Insert Dialog Name Here As Necessary>" Then
If e.EventId Is WindowPattern.WindowOpenedEvent Then
okButtonCondition = New AndCondition(New PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button),
New PropertyCondition(AutomationElement.NameProperty, "OK"))
okButton = sourceElement.FindFirst(TreeScope.Subtree, okButtonCondition)
If okButton IsNot Nothing Then
okButtonInvokePattern = DirectCast(okButton.GetCurrentPattern(InvokePattern.Pattern), InvokePattern)
okButtonInvokePattern.Invoke()
End If
Return
End If
End If
End If
Catch
Return
End Try
End Sub