PDA

View Full Version : When more than "ItemSend" is needed



TechyTommy
01-20-2017, 10:18 AM
Hi could someone help?

I have a Add-in in Outlook (Newforma) that uses the Application_ItemSend function. You can send an email by clicking on Outlooks Send button or on this Addins button.

I wanted to write a small msgbox to remind people to use the Addin when they click on Outlook's Send button.

However because both these buttons trigger the Application_ItemSend my macro runs regardless which button I click.

I need a way to only trigger my code when the Send button is pressed by the user not when the Application_ItemSend function is sent please?

I am a beginner to VBA so if answers could be in full that would be most helpful.

Thank you in advance, here's the code I have done so far:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

If MsgBox("Make sure you are sending using Newforma - Are you sure you want to send this message?", vbYesNo + vbQuestion + vbMsgBoxSetForeground, "WARNING - NEWFORMA BYPASS ?") = vbNo Then

Cancel = True

End If


End Sub

TheSilkCode
01-21-2017, 02:03 AM
Hi-

I would write my code like this:


If MsgBox("Make sure you are sending using Newforma - Are you sure you want to send this message?", vbYesNo) = vbNo Then
Cancel = True
End If

Hope this helps,
TheSilkCode

TechyTommy
01-21-2017, 08:40 AM
Hi thanks for this but it's not exactly what I was asking?


Hi-

I would write my code like this:


If MsgBox("Make sure you are sending using Newforma - Are you sure you want to send this message?", vbYesNo) = vbNo Then
Cancel = True
End If

Hope this helps,
TheSilkCode

TheSilkCode
01-21-2017, 09:56 AM
Well then what are you asking? I thought you said "I wanted to write a small msgbox to remind people to use the Addin when they click on Outlook's Send button." and that's what that does- if user doesn't select ok then cancel is passed during send message and it shouldn't be sent... please explain what part is missing.

TheSilkCode

TechyTommy
01-23-2017, 04:43 AM
However because both these buttons trigger the Application_ItemSend my macro runs regardless which button I click. [/COLOR]

I need a way to only trigger my code when the Send button is pressed by the user not when the Application_ItemSend function is sent please?



Sorry for the confusion?

What I was asking was the above.

skatonni
01-23-2017, 02:51 PM
Try using the Cancel parameter to differentiate Newforma from the Send button.


Private Sub Newforma()
' Cancel = True is a flag indicating Newforma invoked Application_ItemSend
Application_ItemSend CreateItem(0), True
End Sub

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

If Cancel = False Then ' User clicked the Send button
If MsgBox("Make sure you are sending using Newforma - Are you sure you want to send this message?", vbYesNo + vbQuestion + vbMsgBoxSetForeground, "WARNING - NEWFORMA BYPASS ?") = vbNo Then

Cancel = True

End If

Else
' Cancel starts as True when Application_ItemSend was invoked by Newforma
' Reverse the Cancel
Cancel = False

End If

End Sub

TechyTommy
01-24-2017, 03:08 AM
Hi

This hasn't helped I'm afraid? I think again because they both invoke Itemsend both the Newforma button and the standard send button are starting as true and therefore both triggering the Msgbox.

I'm impressed by the concept. Would clicking the Cancel create a event that could be captured?

skatonni
01-24-2017, 10:16 AM
The Application_ItemSend event is described here.

https://msdn.microsoft.com/en-us/library/office/ff865076(v=office.15).aspx

There should be enough detail to see clicking on the Send button cannot set Cancel to True. Mail would never leave Outlook.

TechyTommy
02-01-2017, 06:57 AM
Hi

I have changed the way I wish to do this (as the first way is not working).

I have created a userform with 3 buttons on it - I have never linked my own form to VBA so here goes nothing ...

18211 this is the form

-------------------------------------

In the form I have:
---------------------------------------

Option Explicit


Private Sub CmdNoSendAnyway_Click()
Me.Tag = 3
Me.Hide
End Sub


Private Sub CmdOopsCancel_Click()
Me.Tag = 2
Me.Hide
End Sub


Private Sub CmdYesSend_Click()
Me.Tag = 1
Me.Hide
End Sub


Private Sub Userform_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then Cancel = True
End Sub

------------------------------------

On 'ThisOutlookSession' I have:

-----------------------------------------


Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

Dim myForm As frmNFMsgBox
Set myForm = New frmNFMsgBox
myForm.Show vbModal


Select Case myForm.Tag
Case 1
GoTo lbl_Exit
Case 2
Cancel = True
Case 3
GoTo lbl_Exit
End Select


lbl_Exit:
Unload myForm
Set myForm = Nothing
Exit Sub
End Sub

Is this the best way to do this?

Also the CloseMode private sub is not working? As in if I click the close X nothing happens? I would quite like not to have one really.

any help greatly appreciated,

TT.