There is no Wait Method associated with Application in VisualBasic Word

后端 未结 3 771
执笔经年
执笔经年 2020-12-22 02:55

I have VBA code to run a particular code and I am trying to pause the next execution by giving it a wait time of 3 seconds using the following line:

Applicat         


        
相关标签:
3条回答
  • 2020-12-22 03:37

    It is best to use External Library references (Early Binding) while developing your code. Early Binding has the advantages of both intellisense and Help documentation. A major disadvantage of Early Binding is that it requires the reference to be updated if a different version of the library is installed. This is way it is best to remove the external references and convert code to Late Binding before distributing it.

    Late binding uses CreateObject to import and instantiate a class object.

    CreateObject("Excel.Application").Wait (Now + TimeValue("00:00:05"))  
    

    Alternatively, you could reference the WinApi Sleep function.

    Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long) 
    

    Usage:

     Sleep 3000 '3000 Milliseconds = 3 second delay
    
    0 讨论(0)
  • 2020-12-22 03:41

    This works for me:

    Set wsh = VBA.CreateObject("WScript.Shell")
       Dim waitOnReturn As Boolean: waitOnReturn = True
       Dim windowStyle As Integer: windowStyle = 0
       Dim errorCode As Integer
    
       errorCode = wsh.Run("timeout 5", windowStyle, waitOnReturn)
    

    this code allows to run cmd command and wait until execution is finished, in this particular case I am running command "timeout 5" which simly wait 5 seconds. I've found documentation here

    0 讨论(0)
  • 2020-12-22 03:47

    I tried Excel.Application.Wait(Now + TimeValue("00:00:03")) and it worked like a charm!

    0 讨论(0)
提交回复
热议问题