Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error setting recipients - outlook 2

Status
Not open for further replies.

JTBorton

Technical User
Jun 9, 2008
345
DE
I'm trying to learn how to open a new email message from excel. I am recieveing a run time error when I try to set the recipients. Can anyone see what I am doing wrong?

[!]Run-time error '13'
Type mismatch[/!]

Code:
[COLOR=darkblue]Private Sub[/color] cmdMail_Click()
[COLOR=darkblue]Dim[/color] olApp [COLOR=darkblue]As[/color] Outlook.Application
[COLOR=darkblue]Dim[/color] olMail [COLOR=darkblue]As[/color] MailItem, olSendTo [COLOR=darkblue]As[/color] Recipients

    [COLOR=green]'open Outlook[/color]
    [COLOR=darkblue]Set[/color] olApp = CreateObject("Outlook.Application")
        
    [COLOR=green]'Create mail message[/color]
    [COLOR=darkblue]Set[/color] olMail = olApp.CreateItem(olMailItem)
    
    [COLOR=darkblue]With[/color] olMail
        [highlight]Set olSendTo = .Recipients.Add("test@sbcglobal.net")[/highlight] [!]<-- error[/!]
        olSendTo.Type = olTo
        .Subject = "Test Test Test"
        .body = "message text"
    [COLOR=darkblue]End With[/color]

olMail.Display
 
I see several other people using the exact same code that I have here. I'm wondering if there may be some other setting that is creating the problem. What other settings or factors could impact this?
 
Dim olMail As MailItem, olSendTo As [!]Outlook.[/!]Recipients

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Still giving me the error. It asks if I want to let another program access the outlook contacts then dies.
 
And what about this ?
Code:
Private Sub cmdMail_Click()
Dim olApp As Outlook.Application
Dim olMail As MailItem
    'open Outlook
    Set olApp = CreateObject("Outlook.Application")
    'Create mail message
    Set olMail = olApp.CreateItem(olMailItem)
    With olMail
        .To = "bortonjt@sbcglobal.net"
        .Subject = "Test Test Test"
        .Body = "message text"
        .Display
    End With
End Sub

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
With olMail
.Recipients.Add("bortonjt@sbcglobal.net")

I suspect that Mr. Borton would be less than appreciative of having his e-mail posted willy-nilly on the web for any bot to find.
 
Result:
Okay, both solutions worked well, but PHV's version allows me to add recipients without having to confirm access to the outlook contact list, which is convenient.

Questions:
So if I don't need a recipients variable for this, what role does it play? Were would it be needed?

Also, This procedure only works if outlook is already open. If it is not already open then I get
[!]Runtime error '-2113732605 (82030003)'
Automation error[/!]
Why is that? I can open and create a word document when word is not already open, why not outlook?

-JTBorton
Another Day, Another Disaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top