0
votes

I have to run a vba script as a rule in Outlook that 1. "Catch" specific mail with attachment(Excel) 2. save this attachment as a file and do some changes in this file 3. send by email this "changed" file.

P.1 and P.2 I've already done, but I can't send and email. I try this simple code as a rule when I've got a message with specific words in subject run this script:

 Sub sendemail()
 Dim OutlApp As Object
 On Error Resume Next
  Set OutlApp = GetObject(, "Outlook.Application")<---use this instance of outlook            
 If Err Then
 Set OutlApp = CreateObject("Outlook.Application")
 IsCreated = True
 End If
 OutlApp.Visible = True
 On Error GoTo 0
 With OutlApp.CreateItem(0)
.To = "[email protected]"
.Subject = "test"
.Display    
 End With
 Set OutlApp = Nothing
 End Sub

when I run as a macro (F5) in VBA Project everything went OK, mail appears, but nothing happened when I run the rule. Any Ideas? regards,

1

1 Answers

0
votes

The first line of run a script code has to look like this.

Sub name(itm as mailItem)

or

Sub name(itm as meetingItem)

"itm" is the mail or meeting that is being received.

In your case you must already have another sub where you receive itm and do P1 and P2. Now you would invoke sendemail code after P2.

Sub P1_P2_sendemail(itm As mailItem)

' P1
' P2

With CreateItem(0)
    .To = "[email protected]"
    .Subject = "test"
    .Display
End With

End Sub