0

谁能解释我如何将 for 循环计数器 x 作为过程中 sub 的参数email_send传输application.ontime

找到附件我写的根据不同类型的绘图发送电子邮件提醒的代码。

Dim x As Long

Sub drawings()
    lastrow = Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row

    For x = 2 To lastrow
        If Cells(x, "F") = "Type-1" And Cells(x, "H") = "" Then
            Cells(x, "H").Value = 1
            time1 = Now() + TimeValue("00:02:00")
            Application.OnTime time1, "'email_send" & x & "'"
        ElseIf Cells(x, "F") = "Type-2" And Cells(x, "H") = "" Then
            Cells(x, "H").Value = 1
            time2 = Now() + TimeValue("00:04:00")
            Application.OnTime time2, "'email_send" & x & "'"
        ElseIf Cells(x, "F") = "Type-3" And Cells(x, "H") = "" Then
            Cells(x, "H").Value = 1
            time3 = Now() + TimeValue("00:08:00")        
            Application.OnTime time3, "'email_send" & x & "'"
        ElseIf Cells(x, "F") = "Type-4" And Cells(x, "H") = "" Then
            Cells(x, "H").Value = 1
            time4 = Now() + TimeValue("00:10:00")        
            'time4 = time4 + 5        
            Application.OnTime time4, "'email_send" & x & "'"
            MsgBox time4
        End If

        MsgBox Cells(x, "A")        
    Next x
End Sub



Sub email_send(ByVal x As Long)
    Dim OutApp As Object       
    Dim OutMail As Object
    Dim strbody As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    With OutMail
        'MsgBox "hello"
        .To = Cells(2, 9).Value
        .Subject = "Case ID " & Cells(x, "A") & " (" & Cells(x, "B") & ") Deadline Approaching"
        .Body = "Please complete your assigned drawing asap."
        .Display
        .Send
    End With
End Sub

我是 VBA 新手,所以我请求你们找出我的代码中的错误并提出修改建议。

编辑:感谢您指导我完成这些步骤,但问题是我在遵循这些步骤时遇到了意外错误。我想附上错误的照片,但我不能,因为我没有足够的积分。

4

2 回答 2

0

调用 sub 时,您可以传递参数。

Sub email_send(x as variant) 
'your code here
End sub
x = 3
email_send x

或者您可以将 x 声明为公共变量:
https ://docs.microsoft.com/en-us/office/vba/language/concepts/getting-started/declaring-variables

于 2019-07-04T11:10:36.383 回答
0

首先,您需要更改签名,email_send以便它包含一个接受 Long ...

Sub email_send(ByVal x As Long)

然后你可以按如下方式传递你的论点......

Application.OnTime time1, "'email_send " & x & "'"
于 2019-07-04T12:42:58.647 回答