views:

216

answers:

1

Hi,

I am currently testing how to send an e-mail automatically using VBA within Outlook 2003, to start I have copied the below from the help files:

 Sub CreateHTMLMail()
'Creates a new e-mail item and modifies its properties'

    Dim olApp As Outlook.Application
    Dim objMail As Outlook.MailItem
    Set olApp = Outlook.Application
    'Create e-mail item'
    Set objMail = olApp.CreateItem(olMailItem)

    With objMail
        .Subject = "Test Message"
        .Body = "Body Text"
        .Recipients.Add "[email protected]"
        .Recipients.ResolveAll
        .Display
    End With
End Sub

When I run this I receive a Runtime error '287' message with the .Recipients.Add line highlighted when I select debug. If I take out the .recipients lines the code works and displays a mail with the correct title and body text but I cannot get it to add the recipients. Does anybody know what I am doing wrong?

Thanks in advance for your help. Steve

A: 

Edit:
As the OP states in his comment to my original answer, changing his code to

.Recipients.To = "[email protected]"

solved his problem. I leave my original answer below, because someone may learn from the mistake I made, pointed out by divo ;-)


Original answer (careful, this is wrong!):

Try enclosing the parameters passed to the Add method with parentheses:
.Recipients.Add ("[email protected]")

Treb
Tried that with the same result
Steve
changed .Recipients.Add line to .To = ("[email protected]) and that worked
Steve
@Treb: Since this is VBA there is no need for brackets on a method call. This is one of the biggest misunderstandings in VBA (which actually can introduce bugs).
0xA3
...(continued) See here for an example: http://stackoverflow.com/questions/1070863/hidden-features-of-vba/1070942#1070942
0xA3