Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Cory Nelson   on Feb 15 In MS Office Category.

  
Question Answered By: Earl Stone   on Feb 15

If the messagebox always has the same title (or not always the same, but
something you can predict), you could use AppActivate to check if it exists. In
conjunction with OnTime and SendKeys, you could frequently try to activate the
app with that window (messagebox) open. If it can activate it, then use SendKeys
to send an Enter.

Here is a simple example. Copy the following VBA code into a module in Excel.
Launch MS Word and select File >> Save As. Switch back to Excel and run  the
SendEnter1 macro. As long as cell A1 on Sheet1 is empty, it will try to activate
the Save As window in Word every 10 seconds. If it succeeds, it sends Enter,
which closes the Save As dialog. If it fails (because there is no Save As dialog
present), it send no keystrokes and starts another 10-second countdown. If you
select File >> Save As in Word, within 10 seconds the macro will close it.

Public Sub SendEnter1()
'Call SendEnter2 10 seconds later.
Application.OnTime Now + TimeValue("00:00:10"), "SendEnter2"
End Sub

Private Sub SendEnter2()
'Try to switch to the application with a "Save As" window open.
On Error GoTo DoMore
AppActivate "Save As"
'If we were able to activate the desired app, call SendEnter3
'one second from now.
Application.OnTime Now + TimeValue("00:00:01"), "SendEnter3"
DoMore:
DoEvents
'Quit when anything is entered in A1 on Sheet1 of this workbook.
If Len(ThisWorkbook.Sheets("Sheet1").Range("A1").Value) = 0 Then
'Otherwise, call SendEnter1 to start the proess over again.
Call SendEnter1
End If
End Sub

Private Sub SendEnter3()
'Send an Enter keystroke.
Application.SendKeys "~"
End Sub

Share: 

 

This Question has 9 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Answering message box with visual basic statement Or get search suggestion and latest updates.


Tagged: