Eric said:
The key question here is *when* are you trying to access the Recipients
collection?
I'm trying to access the Recipients collection when the user clicks a
button from an Inspector. The code behind the button writes some
information to a database, based partly on what's in the Recipients
collection, and then sends the email.
Usually the user will enter the address, then the subject, and then the
message body, before clicking my button. If they do things in this
order, everything works fine, because the address will have made its
way into the Recipients collection.
The problem occurs if the last thing they do is to enter or change the
address. If they do that, the address doesn't appear in the Recipients
collection, and isn't processed by my code.
If you need to trap this immediately after someone addresses an
e-mail, then use the PropertyChange event and trap changes to the To property.
After some experimentation, I have found that the "To" property is
updated at the same time as the Recipients collection, so using
PropertyChange won't help.
I'd still like to see your code to provide context.
I can't publish all of my code, but here are some edited highlights:
Option Explicit
Implements IDTExtensibility2
Private WithEvents outlookapp As Outlook.Application
Private WithEvents outlookInspectors As Outlook.Inspectors
Private WithEvents testButton As Office.CommandBarButton
Private Const addinName = "TestAddin.Main"
Private Sub IDTExtensibility2_OnAddInsUpdate(custom() As Variant)
' Do nothing
End Sub
Private Sub IDTExtensibility2_OnBeginShutdown(custom() As Variant)
Set outlookInspectors = Nothing
Set outlookapp = Nothing
Set testButton = Nothing
End Sub
Private Sub IDTExtensibility2_OnConnection( _
ByVal Application As Object, _
ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
ByVal AddInInst As Object, custom() As Variant)
Set outlookapp = Application
Set outlookInspectors = outlookapp.Inspectors
Application.COMAddIns.Item(addinName).object = Me
End Sub
Private Sub IDTExtensibility2_OnDisconnection( _
ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, _
custom() As Variant)
' Do nothing
End Sub
Private Sub IDTExtensibility2_OnStartupComplete(custom() As Variant)
' Do nothing
End Sub
Private Sub outlookInspectors_NewInspector(ByVal Inspector As
Outlook.Inspector)
On Error GoTo error_handler
Dim cmdbar As CommandBar
' Create toolbar buttons if not already present
Set cmdbar = Inspector.CommandBars("Standard")
If testButton Is Nothing Then Set testButton = CreateButton(cmdbar,
"Test button")
cmdbar.Controls("Test button").Visible = True
sub_end:
Set cmdbar = Nothing
Exit Sub
error_handler:
MsgBox "Error displaying email: " & Err.Number & " - " &
Err.Description
On Error Resume Next
Resume sub_end
End Sub
Private Sub testButton_Click(ByVal Ctrl As Office.CommandBarButton,
CancelDefault As Boolean)
Dim r As Outlook.Recipient
Dim Item As Outlook.MailItem
Set Item = ActiveInspector.CurrentItem
MsgBox "Recipient count: " & Item.Recipients.Count
For Each r In Item.Recipients
' More interesting code normally appears here
MsgBox "Recipient: " & r.Address
Next r
Set Item = Nothing
Set r = Nothing
End Sub
Private Function CreateButton(cmdbar As CommandBar, buttonName As
String) As CommandBarButton
Dim btn As CommandBarButton
On Error Resume Next
Set btn = cmdbar.Controls.Item(buttonName)
If Err.Number <> 0 Then
Err.Clear
On Error GoTo 0
Set btn = cmdbar.Controls.Add(msoControlButton)
btn.Caption = buttonName
End If
On Error GoTo 0
With btn
.Style = msoButtonCaption
.Tag = buttonName
.OnAction = "!<" & addinName & ">"
.Visible = False
End With
Set CreateButton = btn
End Function