How to set the Email address in the To field with MailOpen Event

J

Jan T.

I use Office 2000 and try to set an email address on the Open Event
of a new mail. This is my code in ThisOutlookSession:

Dim WithEvents colInsp As Outlook.Inspectors
Dim WithEvents oMail As Outlook.MailItem

Private Sub Application_Startup()
Set colInsp = Application.Inspectors
End Sub

Private Sub colInsp_NewInspector(ByVal Inspector As Inspector)
If Inspector.CurrentItem.Class = olMail Then
Set oMail = Inspector.CurrentItem
End If
End Sub

Private Sub oMail_Open(Cancel As Boolean)
oMail.To = "(e-mail address removed)"
oMail.Subject = "Test"
oMail.Body = "This is a test..."
End Sub

PROBLEM: I can set the Subject and the Body to any text, but I cannot
add an email address to the To field? Why? Is it possible?

What do I need to do to set the email address in the To field?


Regards
Jan
 
K

Ken Slovak

Use the Recipients collection and the Add() method:

Dim colRecips As Outlook.Recipients
Dim oRecip As Outlook.Recipient

Set colRecips = oMail.Recipients
Set oRecip = colRecips.Add( "(e-mail address removed)")
oRecip.Resolve
 
J

Jan T.

Use the Recipients collection and the Add() method:

Dim colRecips As Outlook.Recipients
Dim oRecip As Outlook.Recipient

Set colRecips = oMail.Recipients
Set oRecip = colRecips.Add( "(e-mail address removed)")
oRecip.Resolve

--
Ken Slovak
[MVP - Outlook]http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.http://www.slovaktech.com/products.htm

"Jan T." <noreply> wrote in message



I use Office 2000 and try to set an email address on the Open Event
of a new mail. This is my code in ThisOutlookSession:
Dim WithEvents colInsp As Outlook.Inspectors
Dim WithEvents oMail As Outlook.MailItem
Private Sub Application_Startup()
  Set colInsp = Application.Inspectors
End Sub
Private Sub colInsp_NewInspector(ByVal Inspector As Inspector)
   If Inspector.CurrentItem.Class = olMail Then
       Set oMail = Inspector.CurrentItem
   End If
End Sub
Private Sub oMail_Open(Cancel As Boolean)
   oMail.To = "(e-mail address removed)"
   oMail.Subject = "Test"
   oMail.Body = "This is a test..."
End Sub
PROBLEM: I can set the Subject and the Body to any text, but I cannot
add an email address to the To field? Why? Is it possible?
What do I need to do to set the email address in the To field?
Regards
Jan– Skjul sitert tekst –

– Vis sitert tekst –

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


Well, that does not help, I am afraid. I tried to implement it in
my code, se below, with the new code snippet from you.
The result is the same.
Notice, that if I hit the To button, the email address shows opp
in the To field in the dialog. Also if I cansel this dialog and click
on the Check Names button, imideately the right email address
is showing up? This works for both To="(e-mail address removed)" and
your code using Receipient Collection.

Here is how I implemented your code to my excisting:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - -
Private Sub oMail_Open(Cancel As Boolean)

Dim colRecips As Outlook.Recipients
Dim oRecip As Outlook.Recipient
Set colRecips = oMail.Recipients
Set oRecip = colRecips.Add("(e-mail address removed)")

oRecip.Resolve
' oMail.To = "(e-mail address removed)"
oMail.Subject = "Test"
oMail.Body = "This is a test..."
End Sub
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - -

Any idea?

Jan T.
 
K

Ken Slovak

What doesn't work?

If you use the code as is and click Send, doesn't the email get sent to that
email address?

If not, do you get any errors? If you step the code what happens? Do you get
a Recipient object?



\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\


Well, that does not help, I am afraid. I tried to implement it in
my code, se below, with the new code snippet from you.
The result is the same.
Notice, that if I hit the To button, the email address shows opp
in the To field in the dialog. Also if I cansel this dialog and click
on the Check Names button, imideately the right email address
is showing up? This works for both To="(e-mail address removed)" and
your code using Receipient Collection.

Here is how I implemented your code to my excisting:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - -
Private Sub oMail_Open(Cancel As Boolean)

Dim colRecips As Outlook.Recipients
Dim oRecip As Outlook.Recipient
Set colRecips = oMail.Recipients
Set oRecip = colRecips.Add("(e-mail address removed)")

oRecip.Resolve
' oMail.To = "(e-mail address removed)"
oMail.Subject = "Test"
oMail.Body = "This is a test..."
End Sub
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - -

Any idea?

Jan T.
 
M

Mark

Jan

See the code that I have in the discussion "macro to reply sometimes
fails - why?"

Cheers
Mark
 

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