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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to run a macro automatically when outlook opens 1

Status
Not open for further replies.

IAMINFO

MIS
Feb 21, 2002
62
US
Hello everyone, I have created a macro in outlook 2002 called home. I would like to have outlook run this macro when the application opens, please help. Thank you for taking the time to read this post.

Sub Home()
Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.CreateItem(olMailItem)
myItem.Display '-- Optional --
myItem.To = "psmpis@msn.com"
myItem.Subject = "Home Test"
myItem.Body = "HLS Report."
myItem.Save '-- It's advised to save items before
' adding attachments.
Set myAttachments = myItem.Attachments
myAttachments.Add "C:\home\test.txt"

'myItem.Send '<-- This causes the "click yes" message

'Instead of myItem.Send, use the SendKeys feature.
AppActivate myItem
SendKeys ("%s")
End Sub
 
Use the Application_Startup event
also as your working in Outlook you don't need the line: Set myOlApp = CreateObject("Outlook.Application") instead of myOlApp you can use Application

Code:
Private Sub Application_Startup()
    Dim myItem As MailItem
    Set myItem = Application.CreateItem(olMailItem)
    myItem.To = "psmpis@msn.com"
    myItem.Subject = "Home Test"
    myItem.Body = "HLS Report."
    Set myAttachments = myItem.Attachments
    myAttachments.Add "C:\home\test.txt"
    myItem.Send

End Sub

I don't understand why your saving the item before adding an attachment, using send keys instead of the send event. also i'd consider checking that the attachment exists before attaching.
But with all that said if it works for you, then enjoy.

Never knock on Death's door: ring the bell and run away! Death really hates that!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top