问题
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:
Application.Wait (Now + TimeValue("00:00:03"))
But I get the following error:
Error: Method or data variable not found
回答1:
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
回答2:
I tried Excel.Application.Wait(Now + TimeValue("00:00:03"))
and it worked like a charm!
回答3:
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
来源:https://stackoverflow.com/questions/50957565/there-is-no-wait-method-associated-with-application-in-visualbasic-word