如何安排宏,并继续尝试直到满足条件?

时间:2019-05-17 14:22:26

标签: vba automation scheduled-tasks

尝试根据每天早晨重新发布的csv文件自动发送每日电子邮件,除非不是定期发送。我将代码设置为每天早上09:34:30开始,如果文件不存在,请在10秒钟后重试。

我面临的主要错误是,如果文件不存在,并且重试了代码,那么它将发送多封电子邮件。以下是代码的相关部分(出于隐私原因,我将实际网址删除了):

Sub Main()

Dim origin As Workbook
Dim dest As Workbook

Dim myURL As String


Dim WinHttpReq As Object, oStream As Object
Set WinHttpReq = CreateObject("Microsoft.XMLHTTP")
WinHttpReq.Open "GET", myURL, False
WinHttpReq.Send

If WinHttpReq.status = 404 Then 
    Schedule (Now + TimeValue("00:00:10")) 
    Exit Sub 'tried this to avoid the bug of repeated emails but it's not working
End If

If WinHttpReq.status = 200 Then

    Set oStream = CreateObject("ADODB.Stream")
    oStream.Open
    oStream.Type = 1
    oStream.Write WinHttpReq.ResponseBody
    oStream.SaveToFile ThisWorkbook.Path & "\" & "file.csv", 2
    oStream.Close
    ThisWorkbook.Activate
    ActiveWorkbook.Connections("file").Refresh
    Application.Calculate
    Call Mail.email
    Schedule ("09:34:30")
    Exit Sub


End If


End Sub

Sub Schedule(t As String)
    Application.OnTime t, "Main"
End Sub

1 个答案:

答案 0 :(得分:0)

您可以尝试使用:

public TResponse Call<TResponse, TRequest>(TRequest request)
  where TRequest : IMyClass,IMyOtherClass

它将一直循环播放,直到您的请求状态为200