Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Outlook / Programming VBA / February 2005

Tip: Looking for answers? Try searching our database.

Run macro when new message form is opened?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Reiner Buehl - 03 Feb 2005 10:26 GMT
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.
Sue Mosher [MVP-Outlook] - 03 Feb 2005 12:08 GMT
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.

Signature

Sue Mosher, Outlook MVP
Author of
    Microsoft Outlook Programming - Jumpstart for
    Administrators, Power Users, and Developers
    http://www.outlookcode.com/jumpstart.aspx

> Hi all,
>
[quoted text clipped - 11 lines]
> Best regards,
> Reiner Buehl.
Reiner Buehl - 03 Feb 2005 14:02 GMT
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.

>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.
Sue Mosher [MVP-Outlook] - 03 Feb 2005 14:29 GMT
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
Signature

Sue Mosher, Outlook MVP
Author of
    Microsoft Outlook Programming - Jumpstart for
    Administrators, Power Users, and Developers
    http://www.outlookcode.com/jumpstart.aspx

> Thanks for the reply Sue. Unfortunatly I am not very experienced in this
> area.
[quoted text clipped - 10 lines]
>>item window opens. Use the Class property of Inspector.CurrentItem to
>>determine what kind of item it is.
Reiner Buehl - 03 Feb 2005 15:32 GMT
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 = "user@domain.com"
      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.

>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 [MVP-Outlook] - 03 Feb 2005 15:48 GMT
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.

Signature

Sue Mosher, Outlook MVP
Author of
    Microsoft Outlook Programming - Jumpstart for
    Administrators, Power Users, and Developers
    http://www.outlookcode.com/jumpstart.aspx

> 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
[quoted text clipped - 44 lines]
>>you'd want to build a COM add-in.See
>>http://www.outlookcode.com/d/comaddins.htm
Reiner Buehl - 04 Feb 2005 09:30 GMT
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.

>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 [MVP-Outlook] - 04 Feb 2005 12:14 GMT
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 "me@mydomain.com"
       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.

Signature

Sue Mosher, Outlook MVP
Author of
    Microsoft Outlook Programming - Jumpstart for
    Administrators, Power Users, and Developers
    http://www.outlookcode.com/jumpstart.aspx

> As I tried to explain in my original post, the idea is that the function
> is
[quoted text clipped - 35 lines]
>>
>>If you have some other scenario in mind, please provide specifics.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.