There is no Wait Method associated with Application in VisualBasic Word

时光总嘲笑我的痴心妄想 提交于 2019-12-02 13:59:30

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!