1
votes

Please note this question is related to Outlook I am an amateurish programmer in VB in outlook I want to have a custom Message Box with button captions as 'Send Anyway' and 'Don't Send'.

But with the existing message box changing text is not possible.

So I made a custom form. Now I want to return a Boolean value from the CommandButton1_Click() Sub

This is my main sub which call the form:

Public Result1 As Boolean
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
 Const MAX_ITEM_SIZE As Long = 5242880
 Result1 = True
 Dim FileSize As Long

   For Each Item In Item.Attachments
      FileSize = FileSize + Item.Size      
   Next

           If FileSize > MAX_ITEM_SIZE Then
               UserForm1.Show
               'Cancel = True
               Cancel = Result1
           End If
End Sub

This is my code for click event handler:

Private Sub CommandButton1_Click()
     Unload Me
End Sub

Please advise on how to achieve custom captions on MsgBox Buttons in Outlook

1

1 Answers

1
votes

I'll use the Tag property of the UserForm object to pass back a value from it to its calling sub

this means you'll want to use UserForm Hide() method rather then Unload it from within its code pane, not to loose its state, i.e. all its properties values (and methods calling),

So I'd go like follows:

  • give meaningful names to your Userform1 button

    for instance, let's rename

    • SendBtn, the button that has the "Send Anyway" caption

    • DoNotSendBtn, the button that has the "Don't Send" caption

    you can actually use whatever name you want (even CommandButton1 and CommandButton2 would do), but be consistent with chosen names for their corresponding event handlers names

  • assign them the following click event handlers

    Private Sub DoNotSendBtn_Click() '<--| change "DoNotSendBtn" to your actual chosen button name
        Me.Tag = "True" '<--| store in userform 'Tag' property the value that will be read to cancel the email sending
        Me.Hide '<-- this will hide the userform, thus not loosing its "state" -> 'Tag' property will still be available to the calling sub
    End Sub
    
    Private Sub SendBtn_Click()'<--| change "SendBtn" to your actual chosen button name
        Me.Tag = "False" '<--| store in userform 'Tag' property the value that will be read to let the email sending go on its way
        Me.Hide '<-- this will hide the userform, thus not loosing its "state" ->  'Tag' property will still be available to the calling sub
    End Sub
    
  • finally, change your ItemSend event handler like follows

    Option Explicit
    
    Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
        Const MAX_ITEM_SIZE As Long = 5242880
        Dim FileSize As Long
    
        For Each Item In Item.Attachments
            FileSize = FileSize + Item.Size
        Next
    
        If FileSize > MAX_ITEM_SIZE Then
          UserForm1.Show '<--| show the userform
          Cancel = UserForm1.Tag = "True" '<--| 'Cancel' will be set to 'True' if the userform TAG property value is "True", otherwise it'll be set to 'False'
          Unload UserForm1 '<--| now unload the Userform (and loose its "state", which you don't need any more)
        End If
    End Sub