MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

Getting VBA to wait

  Asked By: Jayden    Date: Sep 11    Category: MS Office    Views: 2120

I am using SendKeys to operate another application (CDex) using VBA.
One part of my programming starts up CDex, and then the procedure
below does some preprocessing before activating CDex and sending it
some key strokes.

You can see from the code that I am attempting to make VBA wait
between activating CDex and sending the first keystroke, and between
keystrokes because it appears that if I do not slow things down, VBA
is sending keystrokes before CDex is ready for it, and these
keystrokes then get ignored. However, the amount of delay needed is
(presumably) dependent on what other things the computer is trying to
do at the same time, so it is hard to judge what to do.

Is there any way of VBA being able to:
(i) check that CDex is properly activated before sending the first
(ii) check that one keystroke has been accepted by CDex before
sending another?

Sub SetupCDex(ErrorStatus)
Dim FiSyOb As Object
Dim FileName As String

Set FiSyOb = CreateObject("Scripting.FileSystemObject")
ErrorStatus = False
On Error GoTo SetErrorStatus

'temporarily rename any mp3 files from before start of conversion
' (and converted audio file) if not already done
FileName = Dir(AudioPathRng.Value, 16)
If FileName <> "" Then
Do While FileName <> ""
If UCase(Right(FileName, 4)) = UCase(".mp3") And
FileDateTime(AudioPathRng.Value & FileName) < StartTime Then
FiSyOb.movefile AudioPathRng.Value & FileName, AudioPathRng.Value &
FileName & "tmp"
FileName = Dir
End If
If FiSyOb.fileexists(AudioPathRng.Value & "converted " &
AudioFileNameRng.Value) Then
FiSyOb.movefile AudioPathRng.Value & "converted " &
AudioFileNameRng.Value, AudioPathRng.Value & "converted " &
AudioFileNameRng.Value & "tmp"
End If

'open dialogue box for audio file conversion
ActivateApplication ("cdex.exe")
Call Delay
SendKeys "%c", True
Call Delay
SendKeys "a", True
Call Delay
SendKeys "{ENTER}", True
GoTo ExitLine

ErrorStatus = True

End Sub

Sub Delay()
Dim i As Long

For i = 1 To 1000000
Next i

End Sub



1 Answer Found

Answer #1    Answered By: Adelinda Fischer     Answered On: Sep 11

This ought a do. You can call this sub procedure from your main code
whenever you need it. Adjust the time  value to the amount  of wait  time

Sub Wait()

Dim CurrentTime As Variant
CurrentTime = Time + TimeValue("0:00:02")
Application.Wait CurrentTime

End Sub

Didn't find what you were looking for? Find more on Getting VBA to wait Or get search suggestion and latest updates.