Run macro when new message form is opened?

R

Reiner Buehl

Hi all,

is there a way to tie a macro to the creation of a new message? I would like
to execute a macro whenever the user creates a new email either by using the
new email or the reply, reply-to-all or forward functionality. The macro
should run like the auto open macros in Word whenever a new mail form is
opened for editing. I want to use this to set the "Have replies send to:"
option for each mail. I have a macro that does this but I do not know how to
invoke it automatically for each new mail.

Best regards,
Reiner Buehl.
 
S

Sue Mosher [MVP-Outlook]

Yes, you can use the Inspectors.NewInspector event to run code when a new
item window opens. Use the Class property of Inspector.CurrentItem to
determine what kind of item it is.
 
R

Reiner Buehl

Thanks for the reply Sue. Unfortunatly I am not very experienced in this area.
From your answer I assume that this can not be done without digging into
VB.net add-in programming? Is this correct? Is there a sample somewhere that
represents the frame for a COM add-in (I again assume that this is what I
need to produce)?

Best regards,
Reiner Buehl.
 
S

Sue Mosher [MVP-Outlook]

The subject of your message refers to a "macro," which implies VBA, which
would work fine for your personal use.

If you want to build something to distribute to other people, then yes,
you'd want to build a COM add-in.See
http://www.outlookcode.com/d/comaddins.htm
--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
R

Reiner Buehl

Then it was a missunderstanding on my side. I only want to use it localy
and I was already affraid off having to use a full development environment
and so on...

I wanted to use the AddReplyRecip function from your Win2000Mag article.
But it seems that my limited VBA knowledge is not good enough. I have added
the function to the code of ThisOutlookSession and added a wrapper based
on some code that I found in the VBA Outlook online help but it doesn't
work:

Sub SetReplyTo()
Dim objApp As Outlook.Application
Dim objInspector As Outlook.Inspector
Dim objItem As MailItem
Dim strRecipName As String

Set objApp = CreateObject("Outlook.Application")
Set objInspector = objApp.ActiveInspector
'Test if an inspector is active
If Not TypeName(objInspector) = "Nothing" Then
Set objItem = objInspector.CurrentItem
strRecipName = "(e-mail address removed)"
If objItem.Class = olMail And _
objItem.Sent = False Then
Call AddReplyRecip(objItem, strRecipName)
End If
End If
Set objItem = Nothing
Set objInspector = Nothing
Set objApp = Nothing
End Sub

Do I need to give the function a special name or did I miss something else?
I am not sure about the part with the check for an active inspector. Do I need
to start one if there is none? And if so, how?

Best regards,
Reiner Buehl.
 
S

Sue Mosher [MVP-Outlook]

The procedure you have below is a macro that can be run from a toolbar
button or with Alt+F8. It is designed to be run while you have an item open.
ActiveInspector will return Nothing if you don't have an item open.

If you have some other scenario in mind, please provide specifics.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
R

Reiner Buehl

As I tried to explain in my original post, the idea is that the function is
executed aoutomatically each time a new, unsent olMail item is created. I want
Outlook to set a reply-to for each outgoing email like most email programs can
do without any programming. The reason for this is that I use Outlook to
connect to two different Exchange instances, the first one is my employers'
system which I use most often, the second one is a customers exchange system
that I connect to when I am onsite there. Since I have no way to check the
exchange account at the customer site when I am not onsite and the exchange
rule that should forward every mail to my main address does not work somehow,
I would like that outlook sets the "Have replies sent to" option always to my
main account so that replys on messages send from the customers account will
not go back to this account but to the one on my employers exchange server
that I can check more often. For this I would need to trigger the macro with
the "new Mail", "Reply", Reply to all" or the forward button so that I do not
have to press a second button for each mail I send. I hope I could explain it
clearer now. Please excuse any mistakes since I am not a native english
speaker.

Best regards,
Reiner.
 
S

Sue Mosher [MVP-Outlook]

The problem is that you aren't keeping enough of the previous posts to make
the whole issue perfectly clear in the current message.

If you want code to run when the user creates a new message, put that code
in the Inspectors.NewInspector event handler:

Dim WithEvents colInsp As Outlook.Inspectors

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

Private Sub colInsp_NewInspector(ByVal Inspector As Inspector)
Dim objMail As Outlook.MailItem
If Inspector.CurrentItem.Class = olMail Then
Set objMail = Inspector.CurrentItem.Class
If objMail.Sent = False Then
objMail.ReplyRecipients.Add "(e-mail address removed)"
End If
End If
Set objMail = Nothing
End Sub

Alternatively, you could perform the same operation when the user sends the
item by putting the code to add the reply recipient in the handler for the
Application.ItemSend event.

--
Sue Mosher, Outlook MVP
Author of
Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

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