空InputBox和错误解决方案

时间:2018-01-30 13:30:34

标签: vba outlook outlook-vba outlook-2010

我试图弄清楚如何确保在每个阶段将数据输入到InputBox中(否则我希望弹出消息并结束输入)。如果没有输入任何内容,我会弹出一个MsgBox但是我想添加一个选项让Sub保留在它所在的InputBox中,这样用户仍然可以输入数据。

我尝试过使用vbRetryCancel并且重试将循环然后从未实际取消(并且我必须强制Outlook关闭以结束宏)或者取消将循环并且它将永远不会重试。如果我可以这样做,那么在输入数据之前,InputBox不会以“Ok”前进,或者单击“cancel”也会起作用,但是如果没有输入任何内容以警告用户,我仍然需要一个MsgBox。

    Sub Request_Tracking()

Dim objMsg As MailItem
Dim tasksource As String
Set objMsg = Application.CreateItem(olMailItem)

'requests input of the user with various options
tasksource = InputBox("Please select task receipt method:" & _
        vbCrLf & vbTab & "1 - Phone call" & _
        vbCrLf & vbTab & "2 - Email" & _
        vbCrLf & vbTab & "3 - Instant Message" & _
        vbCrLf & vbTab & "4 - Desk walk-up" & _
        vbCrLf & vbTab & "5 - Miscellaneous", "Option Chooser")

    'if response is blank then ends the task
    If tasksource = vbNullString Then
        MsgBox "Nothing was entered. Please re-enter your request.", vbOKOnly, "Error!"
        Exit Sub
    End If

    Select Case tasksource
        Case "1"
            tasksource = "Phone call"
        Case "2"
            tasksource = "Email"
        Case "3"
            tasksource = "Instant Message"
        Case "4"
            tasksource = "Desk walk-up"
        Case "5"
            tasksource = "Miscellaneous"
        Case Else
            tasksource = tasksource
    End Select

    'various input boxes for data needed, if response is blank then ends the task without send
    strName = InputBox("Requestor Name:")
        If strName = vbNullString Then
            MsgBox "Nothing was entered. Please reattempt your request.", vbOKOnly, "Error"
            Exit Sub
        End If
    strCoreEvent = InputBox("Task Description:")
        If strCoreEvent = vbNullString Then
            MsgBox "Nothing was entered. Please reattempt your request.", vbOKOnly, "Error"
            Exit Sub
        End If
    strUnitNum = InputBox("Number of units:")
        If strUnitNum = vbNullString Then
            MsgBox "Nothing was entered. Please reattempt your request.", vbOKOnly, "Error"
            Exit Sub
        End If
    strTime = InputBox("Processing Time:")
        If strTime = vbNullString Then
            MsgBox "Nothing was entered. Please reattempt your request.", vbOKOnly, "Error"
            Exit Sub
        End If

    'configures message being sent, sents to WFM box and formats the body font and various line breaks and then sends
    With objMsg
        .To = "tracking@test.com"
        .Subject = "Request Received"
        .HTMLBody = "<body style=font-size:11pt;font-family:Arial><b>For request tracking; please assign to me.</b>" & "<p>" & tasksource & " request from " & strName & ": " & strCoreEvent & "<br />Number of units: " & strUnitNum & "<br />Processing time: " & strTime & "</p></body>"
        .Recipients.ResolveAll
        .Send
    End With

End Sub

2 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

While tasksource = " "
    tasksource = InputBox("Please select task receipt method:" & _
    vbCrLf & vbTab & "1 - Phone call" & _
    vbCrLf & vbTab & "2 - Email" & _
    vbCrLf & vbTab & "3 - Instant Message" & _
    vbCrLf & vbTab & "4 - Desk walk-up" & _
    vbCrLf & vbTab & "5 - Miscellaneous", "Option Chooser", " ")
    If tasksource = vbNullString Then
        Exit Sub
    ElseIf (tasksource = " ") Then
        tasksourceCancel = MsgBox("Nothing was entered. Please re-enter your request. Or press Cancel to leave!", vbOKCancel, "Error!")
        If tasksourceCancel = 2 Then
            Exit Sub
        End If
    End If
Wend

它强制用户输入,如果用户单击“确定”,则返回/默认值将为" " (space)循环,如果用户单击“取消”,则值将为vbNullString,它将退出。 / p>

答案 1 :(得分:0)

尝试这样的事情

Do
    tasksource = InputBox("Please select task receipt method:" & _
            vbCrLf & vbTab & "1 - Phone call" & _
            vbCrLf & vbTab & "2 - Email" & _
            vbCrLf & vbTab & "3 - Instant Message" & _
            vbCrLf & vbTab & "4 - Desk walk-up" & _
            vbCrLf & vbTab & "5 - Miscellaneous", "Option Chooser", 1)
            If Not (tasksource >=1 And tasksource <=5) Then
                optionvar = MsgBox("Incorrect selection. Do you want to re-enter your request?", vbYesNo, "Input required")
                If optionvar = 6 Then 'if yes is pressed
                    tasksource = vbNullString
                Else
                    tasksource = optionvar
                End If
            End If
Loop While tasksource = vbNullString