Outlook COM addin in VB 6.0

M

Moumita

Outlook COM addin in VB 6.0

I hav developed a COM addin in VB 6.0. It sends mails to each of the addresses in the TO field of an email, indivisually .It sometimes behaves in an unpredictable way.It will pick up any one or two of the addresses in the To field of the email and send infinite messages to that mail address.
Sometimes it will pick up people from your personal address lists and send them mails if if none of them are mentioned in the To field of the emails.
It shows this behaviour after a few runs.What could be the reason for it ? Are there any kind of memory leaks? How does the functionality change after a few runs.

I am writing the piece of code also


'Application Outlook - This program makes a button SendMail whenever you
'create a new mail . When we click that button the mail gets sent to each
'of the email addresses individually(i.e separate mails to everyone ).If
'the recepient of the mail is a private distribution list in the contacts
'folder OR its a Public distribution list.This application sends mail to
'each of the members of the lists individually.

Option Explicit

Public oApplication As Outlook.Application
Public WithEvents INewInspectors As Outlook.Inspectors
Dim WithEvents bSendMailButton As CommandBarButton
Public MyMail As MailItem

Private Sub AddinInstance_OnBeginShutdown(custom() As Variant)

On Error Resume Next

Set INewInspectors = Nothing
Set bSendMailButton = Nothing
Set MyMail = Nothing

'Set oApplication = Nothing

End Sub

'This function gets called when the Addin gets loaded on the Application
Public Sub AddinInstance_OnConnection(ByVal oApp As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)

'Initialize the handler which gets called when a new inspector item
'is created

On Error Resume Next
'Print ConnectMode

Set oApplication = oApp

Call Initialize_handler

End Sub

Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)

On Error Resume Next
'bsendButton.Delete

'MsgBox ("the addin has been disconnected from Outlook")

End Sub

' This function is called when we create a new inspector item,i.e. a new mail

Private Sub INewInspectors_NewInspector(ByVal INewInspectors As Outlook.Inspector)

Dim strButtonTag As String

On Error GoTo ErrorOperation

Set MyMail = INewInspectors.CurrentItem

strButtonTag = "SendToAll"

Set bSendMailButton = INewInspectors.CommandBars("Standard").FindControl(msoControlButton, , strButtonTag, , False)

'Adding a button on the standard command bar of the new mail item if one is not already present

If bSendMailButton Is Nothing Then

Set bSendMailButton = INewInspectors.CommandBars("Standard").Controls.Add(msoControlButton, , , , True)

End If

bSendMailButton.Caption = "SendToAll"
bSendMailButton.Tag = strButtonTag
bSendMailButton.Visible = True
bSendMailButton.Style = msoButtonCaption

GoTo NoError

' if the new inspector item is not a mail then it'll go to the msgbox else continue

ErrorOperation:
'MsgBox "this operation valid only on new mail close this and open a new mail"
NoError:

End Sub
Public Sub Initialize_handler()

On Error Resume Next

Set INewInspectors = oApplication.Inspectors

End Sub

'When the Sendmail button on a new mail is clicked.

Public Sub bSendMailButton_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)

Dim Item As ContactItem
Dim folderMAPI As NameSpace
Dim fldContact As Outlook.MAPIFolder
Dim colContacts As Outlook.Items
Dim newmail As MailItem
'These are used as counters in for loop
Dim i As Integer
Dim j As Integer
Dim k As Integer

Dim IContFldItem As Object
Dim IDLstItem As DistListItem

Dim mattach As Outlook.Attachment

On Error Resume Next

If (MsgBox("Are you sure, you want to send this mail Individually ? ", vbYesNo) = vbYes) Then

'MyMail.Recipients.ResolveAll

Set folderMAPI = oApplication.GetNamespace("MAPI")
Set fldContact = folderMAPI.GetDefaultFolder(olFolderContacts)
'for each of the recepients of the mail we'll check whether its a
'private distributionlist a public distribution list or a single
'email address
For i = 1 To MyMail.Recipients.Count


If MyMail.Recipients.Item(i).DisplayType = olPrivateDistList And i <= MyMail.Recipients.Count Then


'If the recepients of the mail is a private distribution list
'then it'll match the name of the private distribution list
'with each of the items in the contacts folder.
'If a match is found then it accesses each of the members of
'the distribtion list.

For Each IContFldItem In fldContact.Items

If IContFldItem = MyMail.Recipients.Item(i).AddressEntry Then

' we cast the recepients item to a distribution list
'item to access all its properties.

Set IDLstItem = IContFldItem

'For each of the memebers in the distribution list we
'send a mail.

For j = 1 To IDLstItem.MemberCount

'Set newmail = Outlook.CreateItem(olMailItem)
Set newmail = MyMail.Copy
newmail.To = IDLstItem.GetMember(j).Address

newmail.Send

Set newmail = Nothing

Next j

End If

Next IContFldItem

End If

'If you're sending the mail to a public distribution list

If MyMail.Recipients.Item(i).DisplayType = olDistList And i <= MyMail.Recipients.Count Then

For k = 1 To MyMail.Recipients.Item(i).AddressEntry.Members.Count

'Creating a new mail for each of the recepients

'Set newmail = Outlook.CreateItem(olMailItem)
Set newmail = MyMail.Copy
newmail.To = MyMail.Recipients.Item(i).AddressEntry.Members.Item(k).Address

newmail.Send

Set newmail = Nothing
Next k

End If

'If the mail recepient is a single user

If MyMail.Recipients.Item(i).DisplayType = olUser And i <= MyMail.Recipients.Count Then

'Creating a new mail

'Set newmail = Outlook.CreateItem(olMailItem)
Set newmail = MyMail.Copy

'if MyMail.Recipients.Item(i).AddressEntry

newmail.To = MyMail.Recipients.Item(i).AddressEntry.Address

'newmail.To = MyMail.Recipients.Item(i).Addres
newmail.Send
Set newmail = Nothing

End If

'If the recepient is a remote user

If MyMail.Recipients.Item(i).DisplayType = olRemoteUser And i <= MyMail.Recipients.Count Then

'Creating a new mail

'Set newmail = Outlook.CreateItem(olMailItem)
Set newmail = MyMail.Copy

newmail.To = MyMail.Recipients.Item(i).AddressEntry.Address

newmail.Send

Set newmail = Nothing

End If

Next i

End If
'Destroying the temporary mail
MyMail.Close (olDiscard)
'oApplication.ActiveInspector.Close (olDiscard)


Set fldContact = Nothing
Set folderMAPI = Nothing
Set colContacts = Nothing
Set IContFldItem = Nothing
Set IDLstItem = Nothing
Set mattach = Nothing
Set newmail = Nothing

End Sub


Plz help.
 
A

Andrew Cushen

Moumita-

Why have you commented out the Resolve statement? You will
have all kinds of problems if you do not resolve all
addresses before you send the e-mails; you shouldn't try
to send e-mail to any address that doesn't resolve
properly.

Hope that sheds some light,

-Andrew
=====================================
-----Original Message-----
Outlook COM addin in VB 6.0

I hav developed a COM addin in VB 6.0. It sends mails to
each of the addresses in the TO field of an email,
indivisually .It sometimes behaves in an unpredictable
way.It will pick up any one or two of the addresses in the
To field of the email and send infinite messages to that
mail address.
Sometimes it will pick up people from your personal
address lists and send them mails if if none of them are
mentioned in the To field of the emails.
It shows this behaviour after a few runs.What could be
the reason for it ? Are there any kind of memory leaks?
How does the functionality change after a few runs.
I am writing the piece of code also


'Application Outlook - This program makes a button SendMail whenever you
'create a new mail . When we click that button the mail gets sent to each
'of the email addresses individually(i.e separate mails to everyone ).If
'the recepient of the mail is a private distribution list in the contacts
'folder OR its a Public distribution list.This application sends mail to
'each of the members of the lists individually.

Option Explicit

Public oApplication As Outlook.Application
Public WithEvents INewInspectors As Outlook.Inspectors
Dim WithEvents bSendMailButton As CommandBarButton
Public MyMail As MailItem

Private Sub AddinInstance_OnBeginShutdown(custom() As Variant)

On Error Resume Next

Set INewInspectors = Nothing
Set bSendMailButton = Nothing
Set MyMail = Nothing

'Set oApplication = Nothing

End Sub

'This function gets called when the Addin gets loaded on the Application
Public Sub AddinInstance_OnConnection(ByVal oApp As
Object, ByVal ConnectMode As
AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As
Object, custom() As Variant)
'Initialize the handler which gets called when a new inspector item
'is created

On Error Resume Next
'Print ConnectMode

Set oApplication = oApp

Call Initialize_handler

End Sub

Private Sub AddinInstance_OnDisconnection(ByVal
RemoveMode As AddInDesignerObjects.ext_DisconnectMode,
custom() As Variant)
On Error Resume Next
'bsendButton.Delete

'MsgBox ("the addin has been disconnected from Outlook")

End Sub

' This function is called when we create a new inspector item,i.e. a new mail

Private Sub INewInspectors_NewInspector(ByVal
INewInspectors As Outlook.Inspector)
Dim strButtonTag As String

On Error GoTo ErrorOperation

Set MyMail = INewInspectors.CurrentItem

strButtonTag = "SendToAll"

Set bSendMailButton = INewInspectors.CommandBars
("Standard").FindControl(msoControlButton, ,
strButtonTag, , False)
'Adding a button on the standard command bar of the
new mail item if one is not already present
If bSendMailButton Is Nothing Then

Set bSendMailButton = INewInspectors.CommandBars
("Standard").Controls.Add(msoControlButton, , , , True)
End If

bSendMailButton.Caption = "SendToAll"
bSendMailButton.Tag = strButtonTag
bSendMailButton.Visible = True
bSendMailButton.Style = msoButtonCaption

GoTo NoError

' if the new inspector item is not a mail then
it'll go to the msgbox else continue
ErrorOperation:
'MsgBox "this operation valid only on new mail
close this and open a new mail"
NoError:

End Sub
Public Sub Initialize_handler()

On Error Resume Next

Set INewInspectors = oApplication.Inspectors

End Sub

'When the Sendmail button on a new mail is clicked.

Public Sub bSendMailButton_Click(ByVal Ctrl As
Office.CommandBarButton, CancelDefault As Boolean)
Dim Item As ContactItem
Dim folderMAPI As NameSpace
Dim fldContact As Outlook.MAPIFolder
Dim colContacts As Outlook.Items
Dim newmail As MailItem
'These are used as counters in for loop
Dim i As Integer
Dim j As Integer
Dim k As Integer

Dim IContFldItem As Object
Dim IDLstItem As DistListItem

Dim mattach As Outlook.Attachment

On Error Resume Next

If (MsgBox("Are you sure, you want to send this mail
Individually ? ", vbYesNo) = vbYes) Then
'MyMail.Recipients.ResolveAll

Set folderMAPI = oApplication.GetNamespace("MAPI")
Set fldContact = folderMAPI.GetDefaultFolder (olFolderContacts)
'for each of the recepients of the mail we'll check whether its a
'private distributionlist a public distribution list or a single
'email address
For i = 1 To MyMail.Recipients.Count


If MyMail.Recipients.Item(i).DisplayType =
olPrivateDistList And i <= MyMail.Recipients.Count Then
'If the recepients of the mail is a private distribution list
'then it'll match the name of the private distribution list
'with each of the items in the contacts folder.
'If a match is found then it accesses each of the members of
'the distribtion list.

For Each IContFldItem In fldContact.Items

If IContFldItem = MyMail.Recipients.Item (i).AddressEntry Then

' we cast the recepients item to a distribution list
'item to access all its properties.

Set IDLstItem = IContFldItem

'For each of the memebers in the distribution list we
'send a mail.

For j = 1 To IDLstItem.MemberCount

'Set newmail = Outlook.CreateItem (olMailItem)
Set newmail = MyMail.Copy
newmail.To = IDLstItem.GetMember (j).Address

newmail.Send

Set newmail = Nothing

Next j

End If

Next IContFldItem

End If

'If you're sending the mail to a public distribution list

If MyMail.Recipients.Item(i).DisplayType = olDistList
And i <= MyMail.Recipients.Count Then
For k = 1 To MyMail.Recipients.Item (i).AddressEntry.Members.Count

'Creating a new mail for each of the recepients

'Set newmail = Outlook.CreateItem(olMailItem)
Set newmail = MyMail.Copy
newmail.To = MyMail.Recipients.Item (i).AddressEntry.Members.Item(k).Address

newmail.Send

Set newmail = Nothing
Next k

End If

'If the mail recepient is a single user

If MyMail.Recipients.Item(i).DisplayType = olUser And
i <= MyMail.Recipients.Count Then
'Creating a new mail

'Set newmail = Outlook.CreateItem(olMailItem)
Set newmail = MyMail.Copy

'if MyMail.Recipients.Item(i).AddressEntry

newmail.To = MyMail.Recipients.Item (i).AddressEntry.Address

'newmail.To = MyMail.Recipients.Item(i).Addres
newmail.Send
Set newmail = Nothing

End If

'If the recepient is a remote user

If MyMail.Recipients.Item(i).DisplayType = olRemoteUser
And i <= MyMail.Recipients.Count Then
 

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