Newly-typed email address not appearing in Recipients collection

A

Andy Bowles

If I do this:

- Create a new email message
- Type an email address into the "To:" field, without moving to
another field
- Click a Commandbar button which runs some VBA

The email address I have just typed doesn't appear in the message's
Recipients collection.

Doing a ResolveAll doesn't seem to help - the newly-typed address just
disappears from the form entirely.

Is there any way I can force Outlook to add the newly-typed address to
the Recipients collection before it does anything else? That is, I
want Outlook to do whatever it would have done if I had moved the focus
to another field.

Outlook Version: 2003 SP2
O/S: XP Home SP2

Thanks.

Andy Bowles
 
G

Guest

Strange. Could you show your code please?

Try creating a new e-mail, add one recipient and run this code:

Dim objMail As Outlook.MailItem
Dim objRs As Outlook.Recipients

Set objMail = ActiveInspector.CurrentItem
Set objRs = objMail.Recipients
Debug.Print objRs.count & " recipients in message"
Set objRs = Nothing
Set objMail = Nothing

Is there anything in the Recipients collection when you run this?
 
A

Andy Bowles

Eric said:
Strange. Could you show your code please?

Try creating a new e-mail, add one recipient and run this code:

Dim objMail As Outlook.MailItem
Dim objRs As Outlook.Recipients

Set objMail = ActiveInspector.CurrentItem
Set objRs = objMail.Recipients
Debug.Print objRs.count & " recipients in message"
Set objRs = Nothing
Set objMail = Nothing

Is there anything in the Recipients collection when you run this?

No - it's empty.

The code which I'm actually trying to make work is part of a COM
Add-in. However, when I came across the problem I tried something
similar to your test code above, and got the same result.

The problem doesn't occur if Outlook has automatically completed the
address, and it doesn't occur if I have clicked outside the To: box.

I have reproduced this behaviour on several different machines. Do
other people get the same thing, or is it just me?

I have been looking for ways to trick Outlook into populating the
Recipients collection. So far I have tried:

(1) objMail.Save

(2) objMail.subject = objMail.subject

(3) objMail.Body = objMail.Body

(4) objMail.HTMLBody = objMail.HTMLBody

(5) ActiveExplorer.Activate
objMail.GetInspector.Activate
DoEvents
SendKeys "+{TAB}"
DoEvents

(1), (2) and (3) have no useful effect.
(4) does what I want, but has the disadvantage of changing the email to
HTML format.
(5) works, but scares me.

Does anyone have any better ideas? For example, is it possible to set
the focus to a different control within the inspector?

Andy Bowles
 
G

Guest

The key question here is *when* are you trying to access the Recipients
collection? 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.

I'd still like to see your code to provide context.
 
A

Andy Bowles

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
 
G

Guest

I don't know what to say Andrew. If I run the code I gave you on a new
e-mail, with an unresolved address in the To: line, and I don't tab away but
instead directly run that macro, the Recipients collection has one item.

I'm fishing here, but is there a chance you are accidentally capturing
another e-mail via ActiveInspector? You may want to consider using an
Inspector Wrapper, which is a best practice for COM Add-Ins, especially when
you're dealing with custom toolbars and menus. There's an example of this
method here:

Slovak Technical Services Code Samples:
http://www.slovaktech.com/code_samples.htm#InspectorWrapper

But there's no guarantee that this will solve your problem, unfortunately.
 
A

Andy Bowles

Eric said:
I don't know what to say Andrew. If I run the code I gave you on a new
e-mail, with an unresolved address in the To: line, and I don't tab away but
instead directly run that macro, the Recipients collection has one item.

Very mysterious.
I'm fishing here, but is there a chance you are accidentally capturing
another e-mail via ActiveInspector?

No, it's definitely the right inspector.
You may want to consider using an
Inspector Wrapper, which is a best practice for COM Add-Ins, especially when
you're dealing with custom toolbars and menus. There's an example of this
method here:

Slovak Technical Services Code Samples:
http://www.slovaktech.com/code_samples.htm#InspectorWrapper

Yes, this does look a more robust way to do things.
But there's no guarantee that this will solve your problem, unfortunately.

No, the problem does seem to be independent of the Add-In. I think
I'll use a variant of the SendKeys kludge I posted earlier.

Many thanks for your help.

Andy Bowles
 

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