none
Dissallow user to click outside the UserForm and also disalow to click on the close (X) button RRS feed

  • Question

  • I have a working piece of code in the ThisOutlookSession that traps an email with ItemSend when the user is sending it. It uses a user form to replace the Subject with a color (red, yellow, or green). I have a couple of questions below.

    1. Is the error handler correctly used in the ItemSend sub below? Is it called on the right place?
    2. The userform has 3 buttons Red, Yellow and Green.  First time I use forms. Is the code ok? Is unload me statement used correctly? Do I need anything else such as initializeForm?
    3. When the form appears I would like the user not to be able to click on the Close (X) button of the form (top right corner), same behavior as the MsgBox is this possible?
    4. When the form appears I would like the user not to be able to click/modify the  email about to be send, actually not to be able to click outside the userform, , same behavior as the MsgBox is this possible?

    ThisOutlookSession Code:

    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
    On Error GoTo Fubar

        ColorForm.Show vbModal

    Exit Sub

    Fubar:

      MsgBox "Error #: " & Err.Number & vbCrLf & Err.Description

    End Sub

    ColorForm Code:

    ‘ Red button

      Private Sub CommandButton1_Click()

      Dim Item As MailItem

      Set Item = Outlook.Application.ActiveInspector.CurrentItem

      Item.Subject = "Red "

      Unload Me

    End Sub

    ‘ Yellow button

    Private Sub CommandButton2_Click()

      Dim Item As MailItem

      Set Item = Outlook.Application.ActiveInspector.CurrentItem

      Item.Subject = "Yellow "

      Unload Me

    End Sub

    ‘ Green button

    Private Sub CommandButton3_Click()

      Dim Item As MailItem

      Set Item = Outlook.Application.ActiveInspector.CurrentItem

      Item.Subject = "Green "

      Unload Me

    End Sub


    Monday, June 9, 2014 11:09 PM

Answers

  • Hello Anastasios, 

    The ItemSend event is not a suitable place for displaying any dialog windows. You shouldn't block the UI thread.

    All the questions below are related to VBA programming. I don't see any Outlook specific questions there.

    >> 1. Is the error handler correctly used in the ItemSend sub below? Is it called on the right place?

    Yes, it is.

    >> 2. The userform has 3 buttons Red, Yellow and Green.  First time I use forms. Is the code ok? Is unload me statement used correctly? Do I need anything else such as initializeForm?

    The unload command is used correctly due to the fact that it is used in the form. If you want to close a form from another module code (or userform), you need to use the Unload function + userformtoclose name.

    >> When the form appears I would like the user not to be able to click on the Close (X) button of the form (top right corner), same behavior as the MsgBox is this possible?

    See Disabling a UserForm's Close Button . Also you may find the Hide close [X] button on excel vba userform for my progress bar thread helpful.

    >> When the form appears I would like the user not to be able to click/modify the  email about to be send, actually not to be able to click outside the userform, , same behavior as the MsgBox is this possible?

    It looks like you need to set the ShowModal property of the form to true.

    Finally, you can read more about VBA programming in the Getting Started with VBA in Outlook 2010 article in MSDN.


    Tuesday, June 10, 2014 12:38 PM

All replies

  • Hello Anastasios, 

    The ItemSend event is not a suitable place for displaying any dialog windows. You shouldn't block the UI thread.

    All the questions below are related to VBA programming. I don't see any Outlook specific questions there.

    >> 1. Is the error handler correctly used in the ItemSend sub below? Is it called on the right place?

    Yes, it is.

    >> 2. The userform has 3 buttons Red, Yellow and Green.  First time I use forms. Is the code ok? Is unload me statement used correctly? Do I need anything else such as initializeForm?

    The unload command is used correctly due to the fact that it is used in the form. If you want to close a form from another module code (or userform), you need to use the Unload function + userformtoclose name.

    >> When the form appears I would like the user not to be able to click on the Close (X) button of the form (top right corner), same behavior as the MsgBox is this possible?

    See Disabling a UserForm's Close Button . Also you may find the Hide close [X] button on excel vba userform for my progress bar thread helpful.

    >> When the form appears I would like the user not to be able to click/modify the  email about to be send, actually not to be able to click outside the userform, , same behavior as the MsgBox is this possible?

    It looks like you need to set the ShowModal property of the form to true.

    Finally, you can read more about VBA programming in the Getting Started with VBA in Outlook 2010 article in MSDN.


    Tuesday, June 10, 2014 12:38 PM
  • Thanks for your reply. 

    You mentioned in your reply that ItemSend event is not a suitable place for displaying any dialog windows.  However I have seen MsgBoxes used in ItemSend event.  Wht do you suggest it is best to use to achieve aformentioned functionality.

    By the way i the form properties Modal it is set to True but still doesnot work.

    Thanks again.

    Tuesday, June 10, 2014 8:28 PM
  • Hi Anastasios,

    Displaying any message boxes is not recommended because they interrupt the Outlook (main) UI thread.

    Try to pass the vbModal value as a parameter to the Show method of the user form. For example:

    UserForm1.Show vbModal

    Wednesday, June 11, 2014 11:27 AM