【问题标题】:How to ensure data is entered into the InputBox?如何确保数据输入到InputBox?
【发布时间】:2018-07-09 09:53:35
【问题描述】:

我正在尝试确保在每个阶段都将数据输入到 InputBox 中,否则我希望弹出消息并结束输入。

如果没有输入任何内容,我会弹出一个 MsgBox,但我想添加一个选项,让 Sub 保留在 InputBox 中,以便用户仍然可以输入数据。

我已经尝试vbRetryCancel 并且重试将循环然后永远不会取消(我必须强制 Outlook 关闭以结束宏)或者取消将循环并且永远不会重试。

如果我能做到,那么在输入数据或单击“取消”之前,InputBox 不会以“Ok”前进,这也可以,但如果没有输入任何内容来提醒用户,我仍然需要一个 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

【问题讨论】:

  • 是否强制用户在输入框中输入数据?
  • @PankajJaju 是的,我愿意。我不确定如何做到这一点的最简单方法

标签: vba outlook outlook-2010


【解决方案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并退出。

【讨论】:

  • 该代码有效,但我遇到的问题是,如果我在 InputBox 上单击取消,则仍会调用“tasksourceCancel”。有没有办法在按下 InputBox 上的取消按钮时退出子而不是额外的步骤?
  • @jnjustice,检查这是否更符合您的要求。
  • @miguelryu 我认为之前的代码更符合我正在寻找的内容,因为我认为警报会更有意义,因此用户知道为什么会出现错误。是否像在输入框中添加“else”功能并包括取消按下一样简单?
  • @jnjustice 您仍然可以将 MsgBox 放在 while(tasksource = vbNullString 之后)来提示错误消息,您要我举例说明吗?
  • @jnjustice,您需要输入else if(tasksource = " "),否则即使tasksource 中有值,该消息也会弹出。
【解决方案2】:

试试这样的

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 2022-01-01
    相关资源
    最近更新 更多