How to check until file exist

怎甘沉沦 提交于 2020-01-14 06:03:49

问题


Is there any way to check until file exists in VBA.

what I am trying to do is, making vba call asynch.

Now after I run

wshShell.Run """" & SFilename & """" & s

I want to check until file exists like this

Wait until fso.fileexists("file")
  Msgbox "file is now available"
End wait!!!

is there any way in vba?

I am using word vba.


回答1:


You can do it like this:

Do

    If fso.FileExists("file") Then

        Exit Do
    End If

    DoEvents 'Prevents Excel from being unresponsive
    Application.Wait Now + TimeValue("0:00:01") 'wait for one second
Loop

MsgBox "file available", vbOKOnly, ""

Although this is surely not the best method


Instead of using Application.Wait, you can use sleep:

Sleep 1000 '1 Second

but you need to add this to your code to be able to use it:

#If VBA7 Then  
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems  
#Else  
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds as Long) 'For 32 Bit Systems  
#End If 


来源:https://stackoverflow.com/questions/37065764/how-to-check-until-file-exist

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