-2

I have the following VBA code:

Set wshshell = CreateObject("WScript.Shell")
wshshell.SendKeys "{F3}"
WScript.Sleep 1000

I get the following error on the WSCript.Sleep line:

Run time error 424: Object required

Community
  • 1
  • 1
dee
  • 35
  • 1
  • 2
  • 6
  • 3
    Could you provide details about your system. The information provided is inadequate for proper analysis. Have a look http://stackoverflow.com/help/how-to-ask at this link – Illegal Argument Jan 14 '17 at 05:48
  • That code works for me - no errors generated - and, if I switch the last two statements around, there is a 1 second delay before the `F3` is performed. – YowE3K Jan 14 '17 at 07:24
  • 1
    Are you trying to do this in VBA or in WScript? – YowE3K Jan 14 '17 at 07:28
  • i am trying to do it in vba – dee Jan 14 '17 at 07:55
  • Possible duplicate of [How to pause for specific amount of time?](http://stackoverflow.com/q/1544526/692942). – user692942 Jan 14 '17 at 11:00
  • This whole thread, question and answers are duplicates. Learn to use search, better yet try this [`site:stackoverflow.com ` prefix on searches in Google](https://www.google.com/?gfe_rd=cr&ei=SAV6WMvVOu3v8AeX-5mAAg&gws_rd=cr&fg=1#q=site:stackoverflow.com+). – user692942 Jan 14 '17 at 11:03
  • @Lankymart I agree that the answers supplied in that duplicate could be used as a workaround - but not one of them explain why `WScript.Sleep 1000` doesn't work, which is what this question is about. – YowE3K Jan 14 '17 at 17:39
  • @YowE3K That's because there not trying to shoehorn a scripting technology into VBA, but take your point. In VBA *(as long as the host supports `Application`)* then `Application.Wait` is the correct approach. – user692942 Jan 14 '17 at 17:42

1 Answers1

1

WScript.Sleep only runs when used inside scripting host controls. VBA doesn't support it. You can, however use the following example to delay.

  #If VBA7 Then
        Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    #Else
        Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    #End If
    Sub Test()

        MsgBox "one"
        Sleep 1000
        MsgBox "Two"
    End Sub
cyboashu
  • 10,196
  • 2
  • 27
  • 46
  • Related - [A: http://stackoverflow.com/questions/1544526/how-to-pause-for-specific-amount-of-time](http://stackoverflow.com/a/23550879/692942) – user692942 Jan 14 '17 at 11:01