ItemSend event fires twice?

B

Bingo

I've the following code to add an additional recipient to
the BCC field. For some reason, this event fires twice.
What's wrong with the code? Thanks.


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

Dim oMapiRecipient As Outlook.Recipient
Dim oMapiItm As Outlook.MailItem
Dim sModName As String
Dim sMailbox As String

On Error GoTo errHandler

Set oMapiItm = Item
sMailbox = "Recipient Name"

' Only outgoing emails need to be copied to Claimbox
If Not oMapiItm.UserProperties("Direction") Is
Nothing And _
oMapiItm.UserProperties("Direction") = "O" Then

Set oMapiRecipient = oMapiItm.Recipients.Add
(sMailbox)

With oMapiRecipient
.Resolve
.Type = olBCC
End With

cleanUp:
Set oMapiRecipient = Nothing
Set oMapiItm = Nothing
Exit Sub
errHandler:
On Error Resume Next
GoTo cleanUp
End Sub
 
M

Michael Bauer

Hi Bingo,

in OL2k I cannot reproduce that behaviour. How do you realize, that it
is firing twice?

Because your code isn´t complete, maybe the main part is missing?
 
B

Bingo

Michael,

The code is a sub from my VB program not the VBA in
OL2K. Maybe that's why? I figured out a work-around by
setting a global bool to turn off the event after the
first firing and then turn it on in the 2nd.

Just do not really understand why the event fires twice.
Thanks.

In case you want to test the sub, first I declare the
events,

Private WithEvents m_oApp As Outlook.Application

then the sub will be triggered.
 
M

Michael Bauer

Sorry, you didn´t answer to my question. I have tried your code and it
works fine.

Because there is one "end if" missing I suppose you have copied a few
parts of your code and *maybe* forgotten something important.

Just another idea: Do you have Norton Internet Security or something
like that running?

--
Viele Grüße
Michael Bauer


Michael,

The code is a sub from my VB program not the VBA in
OL2K. Maybe that's why? I figured out a work-around by
setting a global bool to turn off the event after the
first firing and then turn it on in the 2nd.

Just do not really understand why the event fires twice.
Thanks.

In case you want to test the sub, first I declare the
events,

Private WithEvents m_oApp As Outlook.Application

then the sub will be triggered.
 
B

Bingo

Here's the complete code I have. Thanks.



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

Dim oMapiRecipient As Outlook.Recipient
Dim oMapiItm As Outlook.MailItem
Dim sModName As String
Dim sMailbox As String
Dim sMailID As String

On Error GoTo errHandler

' Here's what I'm doing now to not fire the 2nd event
' To prevent the event fires twice on the same item
If gNoFire Then
gNoFire = False ' to turn on event firing
Exit Sub
End If

sModName = App.EXEName & ":" & CLASS_NAME
& ":ItemSendEvent"

WriteEvent sModName, "ItemSend Event Starts", LOG_INFO

' To get the claimbox name
If Item.UserProperties("Claimbox") Is Nothing Then
sMailbox = m_oLoader.ClaimboxName
Else
If Trim(Item.UserProperties("Claimbox")) <> ""
Then
sMailbox = Trim(Item.UserProperties
("Claimbox"))
Else
sMailbox = m_oLoader.ClaimboxName
End If
End If

' Only outgoing emails need to be copied to Claimbox
If Not Item.UserProperties("Direction") Is Nothing
And _
Item.UserProperties("Direction") = "O" Then

' Clone the message for claimbox
Set oMapiItm = Item.Copy
' Remove all recipients
For Each oMapiRecipient In oMapiItm.Recipients
oMapiRecipient.Delete
Next

' Add the claimbox as the only recipient
Set oMapiRecipient = oMapiItm.Recipients.Add
(sMailbox)
With oMapiRecipient
.Type = olTo
.Resolve
End With

' Send to the claimbox
oMapiItm.Send

' The copy sent out is set to the default message
class
' so all the icons will be reset to the default
Item.MessageClass = "IPM.NOTE"
End If

' turn off event firing
gNoFire = True
cleanUp:
Set oMapiRecipient = Nothing
Set oMapiItm = Nothing
Exit Sub
errHandler:
On Error Resume Next
WriteEvent sModName, "[" & Err.Number & "] " &
Err.Description, LOG_ERROR
GoTo cleanUp
End Sub
 
M

Michael Bauer

Hi Bingo,

you create a copy of the item which is going to send and also send this
copy.

So the first ItemSend is for the original, the second for the copy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top