Wait for Notepad to be opened

robw

Board Regular
Joined
Dec 18, 2002
Messages
161
Hi,
Can someone please help with this.
I don't understand why this bit of VBA is not working as expected.
It should wait until a Notepad window is opened (asynchronously by the user) before continuing.
If a Notepad window is opened BEFORE I execute the code it behaves as expected, but if the Notepad is only opened WHILE the code is executing it does not (it stays in the loop, with eno=5)

Code:
Public Declare Sub sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
Sub gogo()
Dim eno As Integer

'wait until Notepad window is activated (asynchronously by user)

On Error Resume Next
Do
    sleep 1000
    AppActivate "Untitled - Notepad"
    eno = Err.Number
    MsgBox "Err.Number = " & eno
Loop Until eno = 0
       
MsgBox "Notepad is active"

' do things with SendKey
:
:
:

End Sub


Thanks
Rob
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Hi Andrew,
In this context Notepad is a surrogate for our company time-booking application, which is extremely un-user friendly. The objective is to collect/prepare my weekly time-bookings in the more friendly excel environment throughout the week and then, on a Friday, "SendKey" them into the time-booking app. (The app does not accept a simpler cut and paste approach, but I have had some success with using SendKey. I just want to make the whole process slicker)

Regards
Rob
 
Upvote 0
Try adding Err.Clear after the Do line.
 
Upvote 0
You don't need to open Notepad and use SendKeys. You can write to a text file directly in VBA. What is it that you want to put in the text file and where do you want to save it?
 
Upvote 0
Thanks Guys,
Err.Clear fixed it... thanks Rory.
Andrew... not sure if you saw my response to your first query(?). I'm ultimately going to write to a web-based time-booking app, not a text file

Thanks for your help
Cheers
Rob
 
Upvote 0
Rob

If the application you want to write to is web based then there are probably better ways to do this then using the notoriously unreliable SendKeys.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top